Top Banner
Introduction To SQL Unit 13 Modern Business Technology Introduction To TSQL Unit 13 Developed by Michael Hotek
24
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Intro to tsql   unit 13

Introduction To SQLUnit 13

Modern Business Technology

Introduction To TSQLUnit 13

Developed by

Michael Hotek

Page 2: Intro to tsql   unit 13

Cursors

• A cursor is a name that is associated with a select statement

• SQL is a set oriented language which means that an operation is performed on all rows that meet a qualification

• Sometimes, due to business rules, you must process a result set one row at a time

• A cursor gives you this ability

Page 3: Intro to tsql   unit 13

Cursors

• Cursor consist of two parts– cursor result set - result set of the

associated select statement– cursor position - a pointer to the current

row in the result set

• A cursor allows a program to perform an action row by row on a result set instead of on the entire result set

• Provides the ability to delete or update a row based on cursor position

• Bridges the set orientation of an RDBMS and row oriented programming

Page 4: Intro to tsql   unit 13

Cursors

• There are four types of cursors– Language - declared in a batch– Server - declared in a stored procedure– Client - declared in an open client app– Execute - declared in an open client app

• Cursors are handled by SQL Server differently based on the type of cursor

• To the user, the effect of a cursor is the same regardless of type

Page 5: Intro to tsql   unit 13

Cursors

• There are five steps to using a cursor– Declare the cursor for the select

statement you are using– Open the cursor– Fetch each row into the cursor, repeating

until the entire result set has been processed

– Close the cursor– Deallocate the cursor to free up system

resources

Page 6: Intro to tsql   unit 13

Cursors

• The last step is generally forgotten by most developers. Make sure you deallocate any cursors when you have finished with them.

• A cursor is one of the most expensive operation that can be performed in terms of resource usage

• If a cursor is not deallocated, all of the resources that it used are still taken and are not available to any other processes

Page 7: Intro to tsql   unit 13

Declaring Cursors

declare cursor_name cursor for select_statement [for {read only | update [of column_list]}]

declare mycursor cursor for

select * from titles

for read only

• The declare cursor can be the only statement in a batch

• The cursor can have two modes– read only– for update

Page 8: Intro to tsql   unit 13

Declaring Cursors

• The cursor name must be a valid name

• The select statement can consist of any number of clauses

• The select– Must contain a from clause– Can not contain a compute, for browse,

or into– The column_list is the list of columns

defined as updateable

Page 9: Intro to tsql   unit 13

Declaring Cursors

• A cursor that is in read only mode does not allow deletes or updates

• For update is the default mode, but you should always explicitly state what mode the cursor is for

• Regardless of mode, the cursor will be placed in read only mde if the select statement contains– distinct– group by– aggregate functions– unions

Page 10: Intro to tsql   unit 13

Opening Cursors

• After declaring a cursor, the next step is to open the cursor

• Opening the cursor causes the select statement to be executed, make the result set available for processing, and positions the cursor pointer to the first row

declare mycursor cursor for

select * from titles

for read only

open mycursor

Page 11: Intro to tsql   unit 13

Fetching Rows

• Once the cursor is opened, you are ready to begin fetching rows

declare mycursor cursor for

select * from titles

for read only

open mycursor

fetch mycursor

• The cursor determines which row can be updated or deleted based on the cursor position

Page 12: Intro to tsql   unit 13

Fetching Rows

• When you fetch rows, there are two global variables that are important– @@rowcount– @@fetch_status (MS SQL Server)– @@sqlstatus (Sybase)

• As you fetch rows, the value in @@rowcount increases

Page 13: Intro to tsql   unit 13

Fetching Rows

• @@sqlstatus can have three values– 0 successful fetch– 1 error in fetch– 2 no more to fetch

• @@fetch_status can also have three values– 0 successful fetch– -1 error in fetch or no more rows– -2 fetched row doesn't exist

Page 14: Intro to tsql   unit 13

Fetching Rows

• To add to the flexibility, you can fetch the data into variables

fetch cursor_name [into fetch_list]

• The list of variables must match the column list in the select statement

declare @title_name varchar(80)

declare mycursor cursor for select title from titles for read only

open mycursor

fetch mycursor into @title_name

Page 15: Intro to tsql   unit 13

Close and Deallocate

• When you are done with the cursor, close and deallocate it

close cursor_name

deallocate cursor_name

declare mycursor cursor for select title from titles for read only

go

declare @title_name varchar(80)

open mycursor

fetch mycursor into @title_name

close mycursor

deallocate mycursor

Page 16: Intro to tsql   unit 13

Close and Deallocate

• SQL Server will close a cursor when you– exit the session– return from a stored procedure that

declared the cursor

• Do not rely on this to clean up your cursors

• You can reopen a cursor after it has been closed without declaring it again only if you do not deallocate it

open mycursor...

close mycursor

open mycursor…

close mycursor

deallocate mycursor

Page 17: Intro to tsql   unit 13

Example

--The below code will display business books at an 8% increase

declare @title_id char(6),

@type char(12),

@price money

--Declare cursor and perform initial fetch

declare curbooks cursor for select title_id, title, price from titles where type = ‘mod_cook’ for read only

open curbooks

fetch curbooks into @title_id, @type, @price

--Loop through all of the rows

while @@fetch_status = 0

begin

select @title_id, @type, convert(money,@price*1.08)

--Subsequent fetches

fetch curbooks into @title_id, @type, @price

end

close curbooks

deallocate curbooks

--The code below is equivalent to the cursor

select title_id, type, convert(money, price*1.08)

from titles

where type = ‘mod_cook’

Page 18: Intro to tsql   unit 13

Data Modification

• You can delete a row based on the cursor position

• Declare the cursor for update

declare mycursor … for update

open mycursor

fetch mycursor

delete … where current of mycursor

Page 19: Intro to tsql   unit 13

Data Modification

• For data modification based on cursor position, the table requires a unique index

• Even if a cursor is declared for update, you can not delete a row if the cursor's select statement contains a join clause or references a multi-table view

Page 20: Intro to tsql   unit 13

Data Modification

• You can also update data based on cursor position

declare mycursor … for update

open mycursor

fetch mycursor

update … where current of mycursor

Page 21: Intro to tsql   unit 13

Data Modification

• An update does not move the cursor position

• The same row can be updated more than once until the next fetch is performed

• You can update a multi-table view or joined tables only if the update is being performed on one table

Page 22: Intro to tsql   unit 13

Cautions

• Always specify for read only or for update

• Do not leave cursors open for a long time

• If the same operation is performed on every row in the result set, do not use a cursor

• Always close and deallocate your cursors

Page 23: Intro to tsql   unit 13

Unit 13 Review

• Cursor allow you to perform an operation one row at a time

• Cursors can have two modes:– for read only– for update

• The steps to using a cursor are:– declare– open– fetch– close– deallocate

• @@rowcount and @@sqlstatus or @@fetch_status will give you information about your fetch operations

• For more flexibility, you can fetch data into variables

• You can delete or update data by using the where current of cursor_name clause

• Always close and deallocate your cursors

Page 24: Intro to tsql   unit 13

Unit 13 Exercises

• Time allotted for exercises is 1/2 hour