Click here to Skip to main content
15,914,780 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Hi Team,

kindly give me example how to declare cursor and use of cursor.

kindly write a simple query.
Posted

here is another example in SQL 2005 but the syntax should be the same.

http://blog.ysatech.com/post/2009/08/02/SQL-2005-Cursor-Template.aspx[^]
 
Share this answer
 
UPDATE: For working example using AdventureWorks visit : SQL SERVER – Simple Example of Cursor – Sample Cursor Part 2

This is the simplest example of the SQL Server Cursor. I have used this all the time for any use of Cursor in my T-SQL.
DECLARE @AccountID INT
DECLARE @getAccountID CURSOR
SET @getAccountID = CURSOR FOR
SELECT Account_ID
FROM Accounts
OPEN @getAccountID
FETCH NEXT
FROM @getAccountID INTO @AccountID
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @AccountID
FETCH NEXT
FROM @getAccountID INTO @AccountID
END
CLOSE @getAccountID
DEALLOCATE @getAccountID
 
Share this answer
 
Comments
Raj_1984 29-Apr-13 5:15am    
for which purpose DEALLOCATE keyword is used

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900