Follow these steps to create, use, and close a database cursor:
- Create the cursor
- Open the cursor for use within the procedure or application
- Fetch a record's data one row at a time until you have reached the end of the cursor's records
- Close the cursor when you are finished with it
- Deallocate the cursor to completely discard it
Consider the following example of cursor created for pubs..authors
DECLARE @fName VARCHAR(40)
DECLARE @lName VARCHAR(40)
DECLARE @city VARCHAR(40)
DECLARE OAKLAND CURSOR FOR
SELECT au_lname, au_fname, city
FROM pubs..authors
OPEN OAKLAND
FETCH NEXT FROM OAKLAND INTO @fName, @lName, @city
WHILE (@@FETCH_STATUS <> -1)
BEGIN
FETCH NEXT FROM OAKLAND INTO @fName, @lName, @city
IF (@city = 'OAKLAND')
BEGIN
PRINT @city
END
END
GO
CLOSE OAKLAND
DEALLOCATE OAKLAND
GO
You may find more detail use of cursors in online books.
nice to see some thing good about cursors
ReplyDelete