Tuesday, September 13, 2011

Use of #tmp in Staging tables

there were thousands of records in one staging table., what we want is to keep only one record in it and to test one huge query
Two possible ways
1. copy and paste one record in notepad / clipboard., then delete or truncate entire staging table and insert the copied record into staging --> this looks traditional and time consuming
2. here is my way.

SELECT TOP 1 into #tmp 
FROM   [staging table] 

TRUNCATE TABLE [staging table] 

INSERT INTO [staging table] 
SELECT * 
FROM   #tmp 

Cheers,
Vivek

Dont Use other Default Databases

I have got one situation., while configuring service accounts and other accounts, I have given my project database as my default database., later my PM wants to downlod mdf., so I have detached them from SQL... I have closed that session and went for weekend roaming.,
monday when I try logging in I couldn't.. since my login's default db is not available it was not allowing me to login...

here is the trick,
in Login popup, go to optinons and type master or msdb in Database field and try., connect., Managmenet studio would allow you to get in., then from master db, write a query to change your default db

Alter Login [login name] Default database [master]

now all is well ;-)

Online SQL Query Formatter

while surfing., I got some Instant SQL Query formatting and they provide addins too..

sounds pretty interesting., try this...

Beware of Timestamp in Where Clause

I have had to delete records for few dates., I tried the below query and deleted

Select * from [Some Table] where UPDATED_ON_DT = '01-Jan-2011'

but it has deleted rows only for '01-Jan-2011 00 00 000' and left other records
so, the below query solved my issue
Select * from [Some Table] where convert(Date,UPDATED_ON_DT) = '01-Jan-2011'