tsql

I need to insert multiple rows at once in sql server ..based on an iterative value

Hi friends, I want to insert 3 rows at a time in a table based on select statement.. consider the query insert into tblTemp ( a,b ) select a, b from tblTemp2 This lets me insert one row in tblTemp.. my requirement is to add 3 rows with iterative values a,a+1,a+2 for each b inserted. ...

Drawbacks of Dynamic Query in Sqlserver 2005 ?

I have using the many dynamic Query in my database for the procedures because my filter is not fix so i have taken @filter as parameter and pass in the procedure. Declare @query as varchar(8000) Declare @Filter as varchar(1000) set @query = 'Select * from Person.Address where 1=1 and ' + @Filter exec(@query) Like that my filter cont...

T-SQL: if exists always return true ?

Hello, What do you think , does the Stored Procedure always return 1 ? I am concerned about the if exists(..) BEGIN DECLARE @IsUserExisting bit SET NOCOUNT ON IF Exists ( Select null FROM G_User WHERE SamAccountName = @SamAccountName AND NetBIOSDomainName = @NetBIOSDomainName ) BEGIN SET @IsUserExi...

Stored proc executes >30 secs when called from website, but <1 sec when called from ssms

I have a stored procedure that is called by a website to display data. Today the web page has started timing out so I got profiler going and saw the query that was taking too long. I then ran the same query in management studio, under the same user login, and it takes less than a second to return. Is there anything obvious that could be...

SQL Loop over a family tree

Using SQL server 2008. I have a family tree of animals stored in a table, and want to give some information on how 'genetically diverse' (or not) the offspring is. In SQL how can I produce sensible metrics to show how closely related the parents are? Perhaps some sort of percentage of shared blood, or a number of generations to go back b...

Find out which row caused the error

I have a big fat query that's written dynamically to integrate some data. Basically what it does is query some tables, join some other ones, treat some data, and then insert it into a final table. The problem is that there's too much data, and we can't really trust the sources, because there could be some errored or inconsistent data. ...

In a stored procedure how to run a query and conditionally do something

How would I do this in a stored procedure (SQL 2005): count = select count(*) from table1 where line like '%success%' if count > 0: delete from table1 where not line like '%success%' Thanks for any help. My google skills are really failing me today :-( ...

Duplicate values returned with joins

I was wondering if there is a way using TSQL join statement (or any other available option) to only display certain values. I will try and explain exactly what I mean. My database has tables called Job, consign, dechead, decitem. Job, consign, and dechead will only ever have one line per record but decitem can have multiple records all ...

SQL Server Multiple Running Totals

I have a table like this UserID Score Date 5 6 2010-1-1 7 8 2010-1-2 5 4 2010-1-3 6 3 2010-1-4 7 4 2010-1-5 6 1 2010-1-6 I would like to get a table like this UserID Score RunningTotal Date 5 6 6 2010-1-1 5 4 10 2010-1-3 6...

Aggregate survey results recursively by manager

I have a StaffLookup table which looks like this. UserSrn | UserName | ManagerSrn =============================== ABC1 | Jerome | NULL ABC2 | Joe | ABC1 ABC3 | Paul | ABC2 ABC4 | Jack | ABC3 ABC5 | Daniel | ABC3 ABC6 | David | ABC2 ABC7 | Ian | ABC6 ABC8 | Helen | ABC6 The staff stru...

How to quote and reference SQL Server table and field names

Good day! Despite the fact LINQ2SQL and ADO.NET Entity Framework exists there were situations when I need to revert to plain old DataSet (not typed) and friends. While writing SQL for SqlCommand: Is it needed to quote field and table names with []? Is it good to prefix table names with [dbo] I use to use this syntax: SqlCommand co...

Multiple IN statements for WHERE. Would this return good data?

SELECT ['VISA CK - 021810$'].[ACCT NBR #1], ['VISA CK - 021810$'].[ALT CUST NM #1], ['VISA CK - 021810$'].[LAST USED] FROM ['VISA CK - 021810$'] WHERE ['VISA CK - 021810$'].[ALT CUST NM #1] IN ( SELECT ['VISA CK - 021810$'].[ALT CUST NM #1] FROM ['VISA CK - 021810$'] GROUP BY ['VISA CK - 021810...

SQL - How to join on similar (not exact) columns

I have two tables which get updated at almost the exact same time - I need to join on the datetime column. I've tried this: SELECT * FROM A, B WHERE ABS(DATEDIFF(second, A.Date_Time, B.Date_Time)) = ( SELECT MIN(ABS(DATEDIFF(second, A.Date_Time, B2.Date_Time))) FROM B AS B2 ) But it tells me: Multiple columns are specifie...

Performance difference if I UNION first, then put WHERE on unioned result set?

In Microsoft SQL, is there any difference in performance between this: SELECT columns FROM table1 WHERE cond UNION SELECT columns FROM table2 WHERE cond and this: SELECT columns FROM ( SELECT columns FROM table1 UNION SELECT columns FROM table2 ) WHERE cond ? ...

Creating a Function in SQL Server with a Phone Number as a parameter and returns a Random Number

Hi Guys, I am hoping someone can help me here as google is not being as forthcoming as I would have liked. I am relatively new to SQL Server and so this is the first function I have set myself to do. The outline of the function is that it has a Phone number varchar(15) as a parameter, it checks that this number is a proper number, i.e....

Efficiency of checking for null varbinary(max) column ?

Using SQL Server 2008. Example table : CREATE table dbo.blobtest (id int primary key not null, name nvarchar(200) not null, data varbinary(max) null) Example query : select id, name, cast((case when data is null then 0 else 1 end) as bit) as DataExists from dbo.blobtest Now, the query needs to return a "DataExists" column, t...

SQL server datetime column filter on certain date or range of dates

There is an example for today here http://stackoverflow.com/questions/2583228/get-row-where-datetime-column-today-sql-server-noob I am primarily interested in 2008 only. For today it looked like SELECT (list of fields) FROM dbo.YourTable WHERE dateValue BETWEEN CAST(GETDATE() AS DATE) AND DATEADD(DAY, 1, CAST(GETDATE() AS DATE)...

Sql string adding problem

SELECT a.one + ' test ' +b.two from table1 a right join table1 on a.id =b.id The problem is that when one is null then it the whole string is null, is there some kind of trick to bypass this problem msSQL 2005 ...

Finding which table a constraint belongs to

Hi, I need to find out which table(name) a particular constraint belongs to. Does anyone have any TSQL to achieve this? ...

Insert Fails - Detailed Information

Hi, I am loading some data into a temp table. I then use a cursor to loop through the temp table, minipulate the data, and insert it into another table. The identity of the newly inserted record is then captured in a variable and inserted to another table to allow a look up of the newly inserted data. DECLARE c1 CURSOR READ_ONLY FO...