Articles → SQL SERVER → Cursors In SQL Server
Cursors In SQL Server
What Are Cursors?
Steps
Declare cursor_name cursor for select_Statement
fetch next from cursor_name [into variable_name]
while @@fetchstatus = 0
begin
// Do something fetch next
from
cursor_name [into variable_name]
end
Example
Declare @con as varchar(50)
Declare @temp as varchar(50)
Set
@con = ''
Set
@temp = ''
Declare mycursor cursor for
select
sName
from
tbl_Test open mycursor fetch next
from
mycursor into @con while @@fetch_status = 0
begin
Set
@con = @con + @temp fetch next
from
mycursor into @temp
end
close mycursor deallocate mycursor
select
@con