Pages

Google Analytics Alternative

Search This Blog

Wednesday, May 14, 2014

replace cursor with while loop in sql.


Declare @count int;
set @count =1;

Declare @loop int;
declare @tempItem_Id bigint;
select @loop = COUNT(item_id_big) from tbl_item_master;


while @count <= @loop
begin
 WITH MyCte AS
(
    select   item_id_big,
             RowNum = row_number() OVER ( order by item_id_big )
    from     tbl_item_master
)
 select @tempItem_Id = item_id_big from MyCte where RowNum = @count;

 print convert(varchar(15), @tempItem_Id)+ ' '+ convert(varchar(4),@count)  + ' \n ';
 set @count = @count+1;
end

No comments: