Saturday 11 August 2012

How to show record with Serial Number using Cursor in SQL Server 2008

 Show record with Serial Number using Cursor in SQL Server 2008


declare @srno bigint=0
declare @roll bigint
declare @name varchar(50)
declare @lname varchar(50)
declare cursorforsrno cursor fast_forward for select roll,name,lname from stu_info
create table #temp1(sr int,roll int,name varchar(50),lastname varchar(50))
open cursorforsrno
fetch next from cursorforsrno into @roll,@name,@lname
while @@FETCH_STATUS=0
begin
set @srno=@srno+1
insert into #temp1 values(@srno,@roll,@name,@lname)
--select @srno as srno,@roll as roll,@name as name,@lname as [last name]

fetch next from cursorforsrno into @roll,@name,@lname
end
close cursorforsrno
deallocate cursorforsrno
select * from #temp1

0 comments: