sql

Storing Boolean Values In SQL?

I'm designing a SQL datbase table and a couple of the columns need to hold either a 1 or 0 (true or false). I define the columns to be of type binary(1), but now I don't know how to insert a true or false value into the database. inserting "true" or "1" doesn't work (it says either int or bool cannot be converted to binary)... ...

Insert and Relate Records in Single SQL Query?

I am trying to copy data from an old database into a new one, and transform it to follow the new db structure. The old one looked something like this: Table 1: Id | Col A ---------- 1 0 2 8 3 7 4 1 In the new database, Table 1 now looks like this, where the data from col A is now in another table, and it's linked...

Find last record in a single-table chain (SQL Server)

Hey all, Got this table in SQL Server 2005, which is used to maintain a history of merging operations: Column FROM_ID (int) Column TO_ID (int) Now I need a query that takes the original FROM_ID as input, and returns the last available TO_ID. So for instance: ID 1 is merged to ID 2 Later on, ID 2 is merged to ID 3 Again later, ID ...

SQL In Clause with Zero to Many Parameters

I have a SQL query which is parameterized by a very limited in-house framework. The query looks like this: Select * from somewhere where name IN (:parameter); The code will inject zero to many strings into the location specified by :parameter. The ":parameter" flag can only be used within the "IN" clause (so it can't be moved after th...

MySQL: Which indexes to use for a simple range select?

Hello, I have a table with ~30 million rows ( and growing! ) and currently i have some problems with a simple range select. The query, looks like this one: SELECT SUM( CEIL( dlvSize / 100 ) ) as numItems FROM log WHERE timeLogged BETWEEN 1000000 AND 2000000 AND user = 'example'</pre> It takes minutes to finish and i think that the s...

running 7-zip from a SQL script

is it possible to call 7-zip from a SQL script? basically i am extracting tables to csv and then packaging them into 7z files. I have installed 7-zip on the machine where SQL Server is installed and added it to the path, but this is not enough it seems. I get the following error when i try to run the script with xp_cmdshell '7z' is not...

SQL with clause dynamic where parameter

I have a tree-style database with the following structure: Table fields: NodeID int ParentID int Name varchar(40) TreeLevel int I would like to use a variable @NodeID in the first part of the with clause to don't get all the table just start from the piece I'm interested in (see where Parent=@ParentID and comment). with RecursionTe...

star schema design - one column dimensions

Hi guys, I`m new to data warehousing, but I think my question can be relatively easy answered. I built a star schema, with a dimension table 'product'. This table has a column 'PropertyName' and a column 'PropertyValue'. The dimension therefore looks a little like this: surrogate_key | natural_key (productID) | PropertyName | Property...

Mysql: Select where count of one field is greater than one.

I want to do something like: SELECT * FROM db.table WHERE COUNT(someField) > 1 how do I do this in MySql. ...

SQL Server sp_ExecuteSQL and Execution Plans

I have a query which is superfast in SQL Server Management STudio and super slow when run under sp_ExecuteSQL. Is this to do with caching of execution plans not happening when run under spExecuteSQL? ...

How can we set tables relations in SQL Server Express?

I have SQL Server Express 2008. How can I set a table's relations in it? ...

SQL join and aggregate several tables

I have the following tables: users - userid - real name - other stuff roles - roleid - description - other stuff functions - functionid - description screens - screenid - description A user can have multiple roles, as controlled by the table user_roles - userid - roleid Each role can have either edit, view or no acce...

VBA error when executing a sybase stored procedure

Hello, I have the following code that executes a Stored Procedure in Sybase throught VBA. Sub GetInfo() Dim rstRUB As ADODB.Recordset Dim strRUB As String Dim strcnn As String Dim productType As String Dim DealId As String strcnn = "DSN=KONDOR_QUA;DATABASE=Kustom;UID=odbcuser;PWD=odbcuser123;" Set cnn = Ne...

SQL Query: SUM on three columns with criteria

Hi, I have a table with columns like these : idx | amount | usercol1 | usercol2 | usercol3 | percentage1 | percentage2 | percentage3 Data is typically like this : 0 | 1500 | 1 | null | null | 100 | null | null 1 | 3000 | 2 | 3 | null | 50 | 50 | null I would...

T-SQL UNION query to return items with highest and lowest rating from the same table

I want write a stored proc in T-SQL to return the top 5 most highly rated and the bottom 5 most lowly rated articles from an Articles table, determined by the 'rating' column. I was thinking of using a union on two selects but I'm not sure how to write it. ...

SQL show records that don't exist in my table variable

I have a table variable that holds orderID, UnitID and OrderServiceId (it is already populated via a query with insert statement). I then have a query under this that returns 15 columns which also include the OrderId, UnitId, OrderServiceId I need to only return the rows from this query where the same combination of OrderId, UnitId, an...

Execute Stored Procedure in a Cursor

I need to execute stored procedure sp_spaceused for all the tables in my database. I have used cursor for this, please find the below query.The thing is I need to generate report in a single result set. For the below query I'm getting different results. Declare @Name Varchar(500) Declare @GetName Cursor Set @Getname ...

sql to find non-printable characters in a string

Hi, one of my SQL queries is returning non-printable characters in the data because of which I get an error in the report. Please let me know how can I check if a string has a non-printable characters in T-SQL, so that I can find those rows and fix the data? Thanks in advance. ...

Fastest way to update 120 Million records

I need to initialize a new field with the value -1 in a 120 Million record table. Update table set int_field = -1; I let it run for 5 hours before canceling it. I tried running it with transaction level set to read uncommitted with the same results. Recovery Model = Simple. MS SQL Server 2005 Any advice on getting this done faster?...

Criteria Many-to-Many Hibernate

@Entity public class Person implements Serializable { private int id; ........... private Set<Languages> languages = new HashSet<Languages>(); ............... @ManyToMany @JoinTable(name = "link_person_languages") public Set<Languages> getLanguages() { return languages; } } @Entity public...