Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Database Development

G. Mudare

www.belgiumcampus.ac.za
CURSORS
• IMPLEMENTING CURSORS

2
www.belgiumcampus.ac.za
CURSORS
• All data manipulation language statements (SELECT, UPDATE, INSERT, and
DELETE) normally work with entire sets of data at a time.
• Cursors enable you to define a set of data and work with that data one row
at a time
• Some form of a loop to iterate through the set is necessary under these
circumstances is the reason why cursors is called an iterative approach
• As a rule, SQL Server can work with data as a set faster than it can respond
to an iterative series of commands therefore cursors should generally be
avoided
• SQL Server supports server-based cursors, in which the server maintains
the entire set of data that makes up the cursor and keeps track of the
current position in the set of the cursor.

3
www.belgiumcampus.ac.za
DECLARING CURSORS
• To use cursors in TSQL, you must first define the type of cursor and
the data set it operates on
• Many keywords are used in the declaration of a cursor to specify
various options for the cursors behaviour.
• The data set the cursor will work on, is specified by imbedding a
SELECT statement in the declaration.

4
www.belgiumcampus.ac.za
DECLARING CURSORS WITH TSQL
EXTENSIONS
DECLARE cursor name CURSOR
[LOCAL | GLOBAL]
[FORWARD_ONLY | SCROLL]
[STATIC | KEYSET | DYNAMIC]
[READ_ONLY | SCROLL_LOCKS | OPTIMISTIC]
FOR select statement
[FOR UPDATE [OF columns]]
• The keywords LOCAL and GLOBAL define the scope of a cursor.
• LOCAL is the default.
• A LOCAL cursors declaration is only valid within the batch, stored
procedure, or trigger in which it was defined.
5
www.belgiumcampus.ac.za
DECLARING CURSORS WITH TSQL
EXTENSIONS
• A GLOBAL cursor can be used for the duration of the connection in which it was defined.
• If you do not open the cursor within the batch where it is declared, or you reopen the
cursor in a later batch, you will need to declare a global cursor
• FORWARD_ONLY and SCROLL Specifies that the cursor can be positioned forward or
backward and by specific amounts.
• The different options for scrolling are described by the FETCH command
• FORWARD_ONLY cursors will respond only to the FETCH NEXT command whereas
SCROLL cursors can be repositioned in any direction or distance.
• STATIC or KEYSET or DYNAMIC There are three distinct types of cursors allowed in the
TSQL extended syntax: static cursors, keyset driven cursors, and dynamic cursors.
• Each of these types can be specified by the STATIC, KEYSET, or DYNAMIC keywords in the
extended DECLARE CURSOR syntax.

6
www.belgiumcampus.ac.za
DECLARING CURSORS WITH TSQL
EXTENSIONS
• Static. This cursor is the same as an insensitive cursor in SQL-92 standard. It is non-editable, and all values are stored in tempdb
when the cursor is opened.

• Keyset-driven. This cursor has its membership and the order of its value set when the cursor is opened.
➢ A keyset-.driven cursor also uses tempdb; however, only unique row identifiers are stored in tempdb.
➢ In a keyset-driven cursor, inserts to the base tables will be seen through the cursor when the row is scrolled to.
➢ Scrolling to a row that has been deleted will be noted by the system function @@fetch_status being set to a value of –2.
• Dynamic. This is a cursor in which the base tables of the cursor are examined directly.
➢ the full population of the cursor is never fully known.
➢ Scrolling can take place by only a single row at a time, and the row scrolled to is not known until you get there.
➢ new rows can be added and existing rows can be deleted from the base tables throughout the lifetime of the cursor, and
they will be seen as the cursor is repositioned.

7
www.belgiumcampus.ac.za
Syntax to Create Cursor

DECLARE cursorname CURSOR DECLARE @cname NVARCHAR(50)


FOR selectstatement -- like SELECT OPEN ex_cursor
OrderID,CustomerID FROM Orders
FETCH NEXT FROM ex_cursor INTO @oid,@cname
OPEN cursor
WHILE @@FETCH_STATUS = 0
FETCH tablerow FROM cursor
BEGIN
Process Fetched Tablerow
PRINT (CAST(@oid AS VARCHAR(5)) + '-' + @cname)
CLOSE cursor
FETCH NEXT FROM ex_cursor INTO @oid,@cname
DEALLOCATE cursor
END
CLOSE ex_cursor
DEALLOCATE ex_cursor

8
www.belgiumcampus.ac.za
Quiz
Question Output
Write a Cursor driven program, that create labels for =================================
all Employees in column format. NAME:NANCY DAVOLIO
These must include first name and the last name and PHONE:(206) 555-9857
the phone number. =================================
=================================
NAME:ANDREW FULLER
PHONE:(206) 555-9482
=================================
=================================
NAME:JANET LEVERLING
PHONE:(206) 555-3412
=================================

9
www.belgiumcampus.ac.za
Cursor example
• Cursor that create labels for all Employees in column format.
• The include first name and the last name and the phone number.
Use Northwind
DECLARE @fname VARCHAR(20),
@lname VARCHAR(20),
@phone VARCHAR(20)
BEGIN
-- declare CURSOR
DECLARE cur1 CURSOR FOR SELECT FIRSTNAME,LASTNAME, HOMEPHONE FROM Employees;
OPEN cur1
FETCH NEXT FROM cur1 INTO @fname,@lname, @phone;
WHILE @@FETCH_STATUS=0
BEGIN
PRINT '================================='
-- print results
PRINT 'NAME:' + UPPER(@fname + ' ' + @lname);
PRINT 'PHONE:'+ @phone;
FETCH NEXT FROM cur1 INTO @fname,@lname,@phone;
PRINT '================================='
END
CLOSE cur1;
DEALLOCATE cur1;
END

10
www.belgiumcampus.ac.za
Sample Results
=================================
NAME:NANCY DAVOLIO
PHONE:(206) 555-9857
=================================
=================================
NAME:ANDREW FULLER
PHONE:(206) 555-9482
=================================
=================================
NAME:JANET LEVERLING
PHONE:(206) 555-3412
=================================
=================================
NAME:MARGARET PEACOCK
PHONE:(206) 555-8122
=================================

11
www.belgiumcampus.ac.za

You might also like