Is it possible to add index to a temp table? And what’s the difference between create #t and declare @t

#tablename is a physical table, stored in tempdb that the server will drop automatically when the connection that created it is closed, @tablename is a table stored in memory & lives for the lifetime of the batch/procedure that created it, just like a local variable. You can only add a (non PK) index to a … Read more

What is the purpose of putting an ‘N’ in front of function parameters in TSQL?

It indicates a “nationalized” a.k.a. unicode string constant. http://support.microsoft.com/kb/239530 When dealing with Unicode string constants in SQL Server you must precede all Unicode strings with a capital letter N, as documented in the SQL Server Books Online topic “Using Unicode Data”. http://msdn.microsoft.com/en-us/library/aa276823%28SQL.80%29.aspx nchar and nvarchar Character data types that are either fixed-length (nchar) or variable-length … Read more

Can I optimize a SELECT DISTINCT x FROM hugeTable query by creating an index on column x?

This is likely not a problem of indexing, but one of data design. Normalization, to be precise. The fact that you need to query distinct values of a field, and even willing to add an index, is a strong indicator that the field should be normalized into a separate table with a (small) join key. … Read more

Using a join in a merge statement

The query you have will give the error Msg 8156, Level 16, State 1, Line 59 The column ‘AnotherKey’ was specified multiple times for ‘tmpTable’. That is because you are using * in the using clause and AnotherKey is part of both table2 and table3. Specify the columns you need. Also there is no use … Read more

T-SQL INSERT INTO with LEFT JOIN

You want insert into . . . select: INSERT INTO [DB_A].[dbo.a_test](a,b,c,d,e) –ADDED A COLUMN select p.product_info, p.product_date, p.smth, pr.program_name, pr.program_smth FROM [DB_B].dbo.products p LEFT JOIN [DB_B].dbo.program pr ON p.program_name = pr.product_info; I also fixed the query to use table aliases, so it is much easier to read.

Test for Upper Case – T-Sql

Using collations eg: if (‘a’=’A’ Collate Latin1_General_CI_AI) print’same 1′ else print ‘different 1’ if (‘a’=’A’ Collate Latin1_General_CS_AI) print’same 2′ else print ‘different 2’ The CS in the collation name indicates Case Sensitive (and CI, Case Insensitive). The AI/AS relates to accent sensitivity. or in your example SUBSTRING(author,1,1) <> LOWER(SUBSTRING(author,1,1)) COLLATE Latin1_General_CS_AI