tsql

T-SQL Unique constraint locked the SQL server

HI ! This is my table: CREATE TABLE [ORG].[MyTable]( .. [my_column2] UNIQUEIDENTIFIER NOT NULL CONSTRAINT FK_C1 REFERENCES ORG.MyTable2 (my_column2), [my_column3] INT NOT NULL CONSTRAINT FK_C2 REFERENCES ORG.MyTable3 (my_column3) .. ) I've written this constraint to assure that combination my_column2 and my_c...

Hiding result sets from multiple selects in a stored procedure

I have a stored procedure that retrieves SQL queries as text and executes the statements using sp_executesql. Each of the dynamic queries is a count query in that it only returns the number of records found (select COUNT(id) from...). I am looping through a set of SQL queries stored as text and building a table variable out of the result...

Why there is a difference in query results?

SELECT count(*) FROM Table1 t1 SELECT count(*) FROM Table1 t1 Where t1.ID not in (select ID from Table2) SELECT Count(*) FROM Table1 t1 inner join Table2 t2 on t1.ID = t2.ID Results:- 83 62 26 Why there is difference in results of 2nd and 3rd query when total is 83 from Ist Query? Is it not supposed to be either 57 (from 2nd que...

How can i get the seed value of an identity column in MySql

To get the seed and step values of an identity column in sql server i can use this syntax SELECT ColumnName = name, Seed = seed_value, Step = increment_value FROM sys.identity_columns So far in MySql i have found that if i use this syntax SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE auto_increment IS NOT NULL I can at least fi...

SELECT..CASE - Refactor T-SQL

Hello Can I refactor the below SQL CASE statements into single for each case ? SELECT CASE RDV.DOMAIN_CODE WHEN 'L' THEN CN.FAMILY_NAME ELSE NULL END AS [LEGAL_FAMILY_NAME], CASE RDV.DOMAIN_CODE WHEN 'L' THEN CN.GIVEN_NAME ELSE NULL END AS [LEGAL_GIVEN_NAME], CASE RDV.DOMAIN_CODE WHEN 'L' THEN CN.MIDDLE_NAMES ELSE NULL END...

How to list all duplicated rows which may include NULL columns?

Hi guys, I have a problem of listing duplicated rows that include NULL columns. Lemme show my problem first. USE [tempdb]; GO IF OBJECT_ID(N'dbo.t') IS NOT NULL BEGIN DROP TABLE dbo.t END GO CREATE TABLE dbo.t ( a NVARCHAR(8), b NVARCHAR(8) ); GO INSERT t VALUES ('a', 'b'); INSERT t VALUES ('a', 'b'); INSERT t VALUES ('a...

T-SQL - Is there a (free) way to compare data in two tables?

I have table a and table b. (SQL Server 2008) Both tables have the exact same schema. For the purposes of this question, consider table a = my local dev table, table b = the live table. I need to create a SQL script (containing UPDATE/DELETE/INSERT statements) that will update table b to be the same as table a. This script will then b...

T-SQL How To: Compare and List Duplicate Entries in a Table

SQL Server 2000. Single table has a list of users that includes a unique user ID and a non-unique user name. I want to search the table and list out any users that share the same non-unique user name. For example, my table looks like this: ID User Name Name == ========= ==== 0 parker Peter Parker 1 parker Mary...

collation in stored procedure

I have a table which contains data in different languages. All fields are nvarchar(max). I created a stored procedure which trim values of all the fields Create Proc [dbo].[TrimValues] as update testdata set city = dbo.trim(city), state = dbo.trim(state), country = dbo.trim(country), schoolname = dbo.trim(schoolname) after trim all ...

Is there a combination of "LIKE" and "IN" in SQL?

Hi folks. In SQL I (sadly) often have to use "LIKE" conditions due to databases that violate nearly every rule of normalization. I can't change that right now. But that's irrelevant to the question. Further, I often use conditions like WHERE something in (1,1,2,3,5,8,13,21) for better readability and flexibility of my SQL statements. ...

Interesting Row_Number() bug

I was playing with the Stack Exchange Data Explorer and ran this query: http://odata.stackexchange.com/stackoverflow/q/2828/rising-stars-top-50-users-ordered-on-rep-per-day Notice down in the results, rows 11 and 12 have the same value and so are mis-numbered, even though the row_number() function takes the same order by parameter as th...

what data type should I use for storing small decimal value

hi, what data type should I use to store small decimal value? Ranging from -50.00 to 50.00? Thanks, ...

SQL Server Interpolate Missing rows

I have the following table which records a value per day. The problem is that sometimes days are missing. I want to write a SQL query that will: Return the missing days Calculate the missing value using linear interpolation So from the following source table: Date Value -------------------- 2010/01/10 10 2010/01/11 ...

Generate T-SQL for Existing Indexes

How do you programmatically generate T-SQL CREATE statements for existing indexes in a database? SQL Studio provides a "Script Index as->Create to" command that generates code in the form: IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = N'IX_myindex') CREATE NONCLUSTERED INDEX [IX_myindex] ON [dbo].[mytable] ( [my_...

Insert a datetime value with GetDate() function to a SQL server (2005) table?

I am working (or fixing bugs) on an application which was developed in VS 2005 C#. The application saves data to a SQL server 2005. One of insert SQL statement tries to insert a time-stamp value to a field with GetDate() TSQL function as date time value. Insert into table1 (field1, ... fieldDt) values ('value1', ... GetDate()); The re...

SQL Alter Table to Add a Column, does it do anything "scary"?

I need to add a column to an existing table in my live sql database. I know how to use the ALTER command, but what I want to know is the effect this has on the table. For example,, if using SQL Management Studio, it claims that adding a column will "drop and recreate" the table. Will the ALTER table command do that as well? This table...

tsql getdate conversion

Hi, I know that when i do the following, it converts getdate to int select cast (getdate() as int) Getdate output on my server is "2010-06-11 14:42:20.100" and the int to which the above command is converting to is 40339. What is this integer? Did this int consider minutes and seconds? i am confused. Please help. Regards Manjot ...

Find records IN BETWEEN Date Range

Please see attached image I have a table which have FromDate and ToDate. FromDate is start of some event and ToDate is end of that event. I need to find a record if search criteria is in between range of dates. e.g. If a record has FromDate 2010/15/5 and ToDate 2010/15/25 and my criteria is FromDate 2010/5/18 and ToDate is 2010/5/21...

T-SQL Getting duplicate rows returned

The following code section is returning multiple columns for a few records. SELECT a.ClientID,ltrim(rtrim(c.FirstName)) + ' ' + case when c.MiddleName <> '' then ltrim(rtrim(c.MiddleName)) + '. ' else '' end + ltrim(rtrim(c.LastName)) as ClientName, a.MISCode, b.Address, b.City, dbo.ClientGetEnrolledPrograms(CONVERT(int,a.ClientID...

How to update a table with a list of values at a time?

I have update NewLeaderBoards set MonthlyRank=(Select RowNumber() (order by TotalPoints desc) from LeaderBoards) I tried it this way - (Select RowNumber() (order by TotalPoints desc) from LeaderBoards) as NewRanks update NewLeaderBoards set MonthlyRank = NewRanks But it doesnt work for me..Can anyone suggest me how can i perfo...