Tuesday 13 March 2012

While loop in sqlserver

create table #tempWhile
(
[Id] int
,[name] nvarchar(200)
)
insert into #tempWhile values (1,'test1')
insert into #tempWhile values (3,'test3')
insert into #tempWhile values (4,'test4')
insert into #tempWhile values (5,'test5')
-----------------------------------
Declare @id nvarchar(200)
Declare @name nvarchar(200)
-----------------------------------
select Identity(int,1,1) id1, *
into #temp2
from #tempWhile ORDER BY id
----
Declare @count Int
Declare @LoopCount Int
set @count = @@rowcount
set @LoopCount = 1
-----
while @LoopCount <= @count
begin
select @id=id1,@name=name from #temp2 where id = @LoopCount
print 'Name: '+@name+' || ID: '+ @id
-------
set @LoopCount = @LoopCount + 1
end
--------------------------------
Drop table #tempWhile
Drop table #temp2

Get all non-clustered indexes

DECLARE cIX CURSOR FOR     SELECT OBJECT_NAME(SI.Object_ID), SI.Object_ID, SI.Name, SI.Index_ID         FROM Sys.Indexes SI             ...