Cursor Basic – SQL

SQL Server is very good at handling sets of data. For example, you can use a single UPDATE statement to update many rows of data. There are times when you want to loop through a series of rows a perform processing for each row. In this case you can use a cursor.

 

DECLARE @MyID int

DECLARE c1 CURSOR READ_ONLY
FOR
SELECT id
FROM mydata..Mydata

OPEN c1

FETCH NEXT FROM c1
INTO @MyID

WHILE @@FETCH_STATUS = 0
BEGIN

PRINT @MyID

FETCH NEXT FROM c1
INTO @MyID

END

CLOSE c1
DEALLOCATE c1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s