The document discusses cursors in SQL Server 2005, including what they are, their types (forward-only, static, dynamic, keyset driven), and how to work with them using declare, open, fetch, close, and deallocate statements. Cursor operations allow row-by-row processing of result sets for actions like updating or deleting table rows.
7. Types of cursors (Continued) Forward_only cursor is the one which doesn’t support backward scrolling .This cursor only fetches rows in a serial order ,i.e. from beginning of the result set till end. The rows from the database are retrieved only if these are explicitly fetched. The changes made by an INSERT , UPDATE and DELETE operations are reflected only when the rows are fetched from cursors Forward_ only It is a cursor that does not reflect any kind of modification that has been made either to the associated tables in the database or to the result set of the cursor itself. Hence, none of the UPDATE,DELETE or INSERT operations is reflected in the cursor unless it is closed and reopened Static Description Cursor Type
8. Types of Cursors (Continued) Description Cursor Type Dynamic cursors are cursors that reflect all the changes that have been made to the result set while scrolling through it . All changes caused by any of the INSERT, UPDATE or DELETE operations are reflected in these cursors Dynamic keyset driven cursors are cursors that show changes in the result set caused by UPDATE and DELETE statement while the cursor is open but do not reflect the changes caused by the INSERT operation Keyset driven
Faculty Notes : SQL server is very good at handling sets of data. For example : User can use a single SELECT statement to retrieve multiple rows of data. There are times when user wants to loop through a series of records to perform processing for each record .In this case user requires a cursor.
Faculty notes: Static cursors are read only cursors .The result set of a static cursor is built in the tempdb database when it is opened.
Faculty Notes : Cursor_name is the name of the cursor INSENSITIVE causes the result set to be stored in the tempdb database and does not permit the result set of the cursor to be modified. SCROLL specifies that the cursor can be positioned forwarded or backward to read the data. All the subsequent fetched display the modifications that have been made to underlying tables as long as the cursor is not declared using the INSENSITIVE option Select statement is a standard SELECT statement used for reterving rows from one or more tables. Prepared statement is the name of the prepared SQL SELECT statement READ ONLY prevents modification from taking place in the result set of the cursor. UPDATE[OF COLUMN_LIST] DEFINES THE UPDATEABLE COLUMNS WITHIN THE CURSOR. If the OF column_list option is used, then only the listed columns can be updated else all columns are updatable, unless the cursor has been defined as READ ONLY
FACULTY NOTES: NEXT moves the cursor one row forward in the result set. This is the default action performed by the fetch statement. PRIOR returns the previous row in the result set and return it. FIRST moves the cursor to the first row in the result set and return it. LAST moves the cursor to the last row in the result set and return it FROM CURSOR_NAME is the name of the cursor from which the row(s) is to be fetched. INTO hvar are one or more host variables that store values returned by the FETECH statement. The number and the data type of the host variable must be identical to that of the columns used in the fetch statement. When a FETCH statement is executed the cursor is said to be positioned on the row that has been fetched. The fetched row is called the current row .If the cursor is not a READ ONLY cursor, then the current row in the underlying table can be modified or deleted by using the WHERE CURRENT OF clause. These operations on the current row are referred to as positioned operations. Closing Cursor: When a close statement is executed it leaves the structure of the cursor accessible for reopening However, the fetches and modifications are not possible unless the cursor is reopened.
Faculty notes : The first Declare statement sets aside storage for one variables The OPEN statement gets the rows that the cursor declares The first FETCH NEXT statement gets the first row from the cursor @@fetch_status is a variable which will contain the value 0 if the previous fetch was successful. The print statement formats and prints the selected data for the user.
Faculty notes The above example displays the value -2 for @@fetch_status, which means as the row for the id=2 is deleted and when you try to fetch that row ,it sets the value -2 to @@fetch_status. Before creating the above cursor, execute the following queries: create table report(id int primary key, name varchar(50)) insert into report values(1,'john') insert into report values(2,'tom') insert into report values(3,'peter') select * from report