Wednesday, August 25, 2010

How to get missing numbers of a given sequence of numbers. Eg: 1,2,4,5,7,9,11,12 Output Should be : 3,6,8,10

create table Sequence_Numbers (
id int not null primary key
);

insert into Sequence_Numbers(id) values
(1), (2), (3), (4), (6), (7), (8), (9),
(10), (15), (16), (17), (18), (19), (20);


select * from Sequence_Numbers

select l.id + 1 as start
from Sequence_Numbers as l
left outer join Sequence_Numbers as r on l.id + 1 = r.id
where r.id is null;

Saturday, June 26, 2010

SQLCMD - Examples

sqlcmd Switches
-S >> ServerName
-D >> DatabaseName
-U >> UserName
-P >> Password
-Q >> Query
-o >> OuputFile

sqlcmd -SSQL2005_FIRST -Ucalyan -Pcalyan
-dDatabaseA -Q"select * from sampledata"

sqlcmd -SSQL2005_FIRST -Ucalyan -Pcalyan
-dDatabaseA -Q"select * from sampledata" -o"c:\sampledata.txt"


sqlcmd -SSQL2005_FIRST -Ucalyan -Pcalyan
-dDatabaseA -Q"EXEC MYPROC1"

sqlcmd -SHOME-CB\SQL2005_FIRST -Ucalyan -Pcalyan
-dDatabaseA -Q"exec msdb.dbo.sp_start_job @job_name = 'TestJob'"

Tuesday, June 1, 2010

sys.indexes vs sysindexes -- Explanation

Generally everybody use both the commands, here is the explanation.

sysindexes -- compatibility views

sys.indexes -- catalog view

Generally we should avoid using sysindexes kind of compatibility views. Microsoft keep this views is becoz to make backward compatibility while migrating from one version to another version.