Does any have experience in creating cursor to loop through a bunch of records? If you do can you provide me with a basic example. Thanks
Normally, when we need data looping, we use either "Cursors" or "While loop" in SQL Server. Both are used with multiple rows to give decisions on a row-by-row basis. Cursors - Cursor is a database object used by applications to manipulate the data in a set on a row-by-row basis.
For more information on cursors, also take a look at the free SQL query training provided by Steve Stedman. In SQL Server the cursor is a tool that is used to iterate over a result set, or to loop through each row of a result set one row at a time.
declare cur cursor for
select id from tbl
open cur
declare @id int
fetch next from cur into @id
while (@@FETCH_STATUS = 0)
begin
print(@id)
fetch next from cur into @id
end
close cur
deallocate cur
-- just replace "tbl" with your table name and "id" with your field name
-- and do whatever you want in begin-end block (now it simply prints the id of each record)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With