A database cursor is similar to the cursor on a word processor screen. As you press the down arrow key, the cursor scrolls down through the text one line at a time. Pressing the Up Arrow key scrolls your cursor up one line at a time. Pressing other keys such as Page Up and Page Down results in a leap of several lines in either direction. Database cursors operate in the same way.
Database cursors enable you to select a group of data, scroll through the group of records(often called a recordset), and example each individual line of data as the cursor points to it. You can use a combination of local variables and a cursor to individually examine each record and perform any external operations needed before moving on to the next record.yo
One other common use of cursors is to save a query's results for later use. A cursor's result set is created from the result set of a SELECT query. If your application or procedure requires the repeated use of a set of records, it is faster to create a cursor once and reuse it several times than to repeatedly query the database.
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.
Creating a Cursor
To create a cursor using Transact-SQL, issue the following syntax:By executing the declare cursor_name CURSOR statement, you have defined the cursor result set that will be used for all your cursor operations. A cursor has two important parts: the cursor result set and the cursor position.
The following statement creates a cursor based on the ARTISTS table:
You now have a simple cursor object named Artists_Cursor that contains all the records in the ARTISTS table. But first you must open the cursor.
Opening a Cursor
The simple command to open a cursor for use is,
OPEN CURSOR_NAME
Executing the following statement opens Artists_Cursor for use:
Now you can use the cursor to scroll through the result set.
Scrolling a Cursor
To scroll the cursors result set, Transact-SQL provides the following FETCH command:
FETCH CURSOR_NAME [INTO FETCH_TARGET_LIST]
Each time the FETCH command is executed, the cursor pointer advances through the result set one row at a time. If desired, data from each row can be fetched into the FETCH_TARGET_LIST variables.
The following statements fetch the data from the Artists_Cursor result set and return the data to the program variables:
You can use WHILE loop to loop through the entire result set. But how do you know when you have reached the end of the records?
Testing a Cursors Status
Transact-SQL enables you to check the status of the cursor at any time through the maintenance of two global variables: @@FETCH_STATUS and @@ROWCOUNT. This variable contains one of three values.
Status - 0: Successful completion of the FETCH statement.
Status - 1: The FETCH statement resulted in an error.
Status - 2: There is no more data in the result set.
Now you have a fully functioning cursor! The only step left is to close the cursor.
Closing a Cursor
Closing a cursor is very simple. The statement to close a cursor is as follows:close cursor_name
This cursor still exists. however, it must be reopened. Closing a cursor essentially closes out its result set, not its entire existence. When you are completely finished with a cursor, the DEALLOCATE command frees the memory associated with a cursor and frees the cursor name for reuse. The DEALLOCATE statement syntax is as follows:
DEALLOCATE CURSOR_NAME
You can use the following example for complete process of creating a cursor,
Examples:
DECLARE @NAME VARCHAR(250)
DECLARE @STYLE VARCHAR(150)
DECLARE @ARTISTS_ID INT
DECLARE ARTISTS_CURSOR CURSOR
FOR SELECT * FROM ARTISTS
OPEN ARTISTS_CURSOR
FETCH ARTISTS_CURSOR INTO @NAME, @STYLE, @ARTISTS_ID
WHILE(@@FETCH_STATUS=0)
BEGIN
PRINT @NAME
PRINT @STYLE
PRINT CHAR(@ARTISTS_ID)
FETCH ARTISTS_CURSOR INTO @NAME, @STYLE, @ARTISTS_ID
END
CLOSE ARTISTS_CURSOR
DEALLOCATE ARTISTS_CURSOR
GO
DECLARE @STYLE VARCHAR(150)
DECLARE @ARTISTS_ID INT
DECLARE ARTISTS_CURSOR CURSOR
FOR SELECT * FROM ARTISTS
OPEN ARTISTS_CURSOR
FETCH ARTISTS_CURSOR INTO @NAME, @STYLE, @ARTISTS_ID
WHILE(@@FETCH_STATUS=0)
BEGIN
PRINT @NAME
PRINT @STYLE
PRINT CHAR(@ARTISTS_ID)
FETCH ARTISTS_CURSOR INTO @NAME, @STYLE, @ARTISTS_ID
END
CLOSE ARTISTS_CURSOR
DEALLOCATE ARTISTS_CURSOR
GO
Comments