Successfully reported this slideshow.

Intro to tsql unit 13


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Intro to tsql unit 13

  1. 1. Introduction To SQL Unit 13 Modern Business Technology Introduction To TSQL Unit 13 Developed by Michael Hotek
  2. 2. Cursors <ul><li>A cursor is a name that is associated with a select statement </li></ul><ul><li>SQL is a set oriented language which means that an operation is performed on all rows that meet a qualification </li></ul><ul><li>Sometimes, due to business rules, you must process a result set one row at a time </li></ul><ul><li>A cursor gives you this ability </li></ul>
  3. 3. Cursors <ul><li>Cursor consist of two parts </li></ul><ul><ul><li>cursor result set - result set of the associated select statement </li></ul></ul><ul><ul><li>cursor position - a pointer to the current row in the result set </li></ul></ul><ul><li>A cursor allows a program to perform an action row by row on a result set instead of on the entire result set </li></ul><ul><li>Provides the ability to delete or update a row based on cursor position </li></ul><ul><li>Bridges the set orientation of an RDBMS and row oriented programming </li></ul>
  4. 4. Cursors <ul><li>There are four types of cursors </li></ul><ul><ul><li>Language - declared in a batch </li></ul></ul><ul><ul><li>Server - declared in a stored procedure </li></ul></ul><ul><ul><li>Client - declared in an open client app </li></ul></ul><ul><ul><li>Execute - declared in an open client app </li></ul></ul><ul><li>Cursors are handled by SQL Server differently based on the type of cursor </li></ul><ul><li>To the user, the effect of a cursor is the same regardless of type </li></ul>
  5. 5. Cursors <ul><li>There are five steps to using a cursor </li></ul><ul><ul><li>Declare the cursor for the select statement you are using </li></ul></ul><ul><ul><li>Open the cursor </li></ul></ul><ul><ul><li>Fetch each row into the cursor, repeating until the entire result set has been processed </li></ul></ul><ul><ul><li>Close the cursor </li></ul></ul><ul><ul><li>Deallocate the cursor to free up system resources </li></ul></ul>
  6. 6. Cursors <ul><li>The last step is generally forgotten by most developers. Make sure you deallocate any cursors when you have finished with them. </li></ul><ul><li>A cursor is one of the most expensive operation that can be performed in terms of resource usage </li></ul><ul><li>If a cursor is not deallocated, all of the resources that it used are still taken and are not available to any other processes </li></ul>
  7. 7. Declaring Cursors <ul><li>declare cursor_name cursor for select_statement [for {read only | update [of column_list]}] </li></ul><ul><li>declare mycursor cursor for </li></ul><ul><li>select * from titles </li></ul><ul><li>for read only </li></ul><ul><li>The declare cursor can be the only statement in a batch </li></ul><ul><li>The cursor can have two modes </li></ul><ul><ul><li>read only </li></ul></ul><ul><ul><li>for update </li></ul></ul>
  8. 8. Declaring Cursors <ul><li>The cursor name must be a valid name </li></ul><ul><li>The select statement can consist of any number of clauses </li></ul><ul><li>The select </li></ul><ul><ul><li>Must contain a from clause </li></ul></ul><ul><ul><li>Can not contain a compute, for browse, or into </li></ul></ul><ul><ul><li>The column_list is the list of columns defined as updateable </li></ul></ul>
  9. 9. Declaring Cursors <ul><li>A cursor that is in read only mode does not allow deletes or updates </li></ul><ul><li>For update is the default mode, but you should always explicitly state what mode the cursor is for </li></ul><ul><li>Regardless of mode, the cursor will be placed in read only mde if the select statement contains </li></ul><ul><ul><li>distinct </li></ul></ul><ul><ul><li>group by </li></ul></ul><ul><ul><li>aggregate functions </li></ul></ul><ul><ul><li>unions </li></ul></ul>
  10. 10. Opening Cursors <ul><li>After declaring a cursor, the next step is to open the cursor </li></ul><ul><li>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 </li></ul><ul><li>declare mycursor cursor for </li></ul><ul><li>select * from titles </li></ul><ul><li>for read only </li></ul><ul><li>open mycursor </li></ul>
  11. 11. Fetching Rows <ul><li>Once the cursor is opened, you are ready to begin fetching rows </li></ul><ul><li>declare mycursor cursor for </li></ul><ul><li>select * from titles </li></ul><ul><li>for read only </li></ul><ul><li>open mycursor </li></ul><ul><li>fetch mycursor </li></ul><ul><li>The cursor determines which row can be updated or deleted based on the cursor position </li></ul>
  12. 12. Fetching Rows <ul><li>When you fetch rows, there are two global variables that are important </li></ul><ul><ul><li>@@rowcount </li></ul></ul><ul><ul><li>@@fetch_status (MS SQL Server) </li></ul></ul><ul><ul><li>@@sqlstatus (Sybase) </li></ul></ul><ul><li>As you fetch rows, the value in @@rowcount increases </li></ul>
  13. 13. Fetching Rows <ul><li>@@sqlstatus can have three values </li></ul><ul><ul><li>0 successful fetch </li></ul></ul><ul><ul><li>1 error in fetch </li></ul></ul><ul><ul><li>2 no more to fetch </li></ul></ul><ul><li>@@fetch_status can also have three values </li></ul><ul><ul><li>0 successful fetch </li></ul></ul><ul><ul><li>-1 error in fetch or no more rows </li></ul></ul><ul><ul><li>-2 fetched row doesn't exist </li></ul></ul>
  14. 14. Fetching Rows <ul><li>To add to the flexibility, you can fetch the data into variables </li></ul><ul><li>fetch cursor_name [into fetch_list] </li></ul><ul><li>The list of variables must match the column list in the select statement </li></ul><ul><li>declare @title_name varchar(80) </li></ul><ul><li>declare mycursor cursor for select title from titles for read only </li></ul><ul><li>open mycursor </li></ul><ul><li>fetch mycursor into @title_name </li></ul>
  15. 15. Close and Deallocate <ul><li>When you are done with the cursor, close and deallocate it </li></ul><ul><li>close cursor_name </li></ul><ul><li>deallocate cursor_name </li></ul><ul><li>declare mycursor cursor for select title from titles for read only </li></ul><ul><li>go </li></ul><ul><li>declare @title_name varchar(80) </li></ul><ul><li>open mycursor </li></ul><ul><li>fetch mycursor into @title_name </li></ul><ul><li>close mycursor </li></ul><ul><li>deallocate mycursor </li></ul>
  16. 16. Close and Deallocate <ul><li>SQL Server will close a cursor when you </li></ul><ul><ul><li>exit the session </li></ul></ul><ul><ul><li>return from a stored procedure that declared the cursor </li></ul></ul><ul><li>Do not rely on this to clean up your cursors </li></ul><ul><li>You can reopen a cursor after it has been closed without declaring it again only if you do not deallocate it </li></ul><ul><li>open mycursor... </li></ul><ul><li>close mycursor </li></ul><ul><li>open mycursor… </li></ul><ul><li>close mycursor </li></ul><ul><li>deallocate mycursor </li></ul>
  17. 17. Example <ul><li>--The below code will display business books at an 8% increase </li></ul><ul><li>declare @title_id char(6), </li></ul><ul><ul><li>@type char(12), </li></ul></ul><ul><ul><li>@price money </li></ul></ul><ul><li>--Declare cursor and perform initial fetch </li></ul><ul><li>declare curbooks cursor for select title_id, title, price from titles where type = ‘mod_cook’ for read only </li></ul><ul><li>open curbooks </li></ul><ul><li>fetch curbooks into @title_id, @type, @price </li></ul><ul><li>--Loop through all of the rows </li></ul><ul><li>while @@fetch_status = 0 </li></ul><ul><li>begin </li></ul><ul><li>select @title_id, @type, convert(money,@price*1.08) </li></ul><ul><ul><li>--Subsequent fetches </li></ul></ul><ul><ul><li>fetch curbooks into @title_id, @type, @price </li></ul></ul><ul><li>end </li></ul><ul><li>close curbooks </li></ul><ul><li>deallocate curbooks </li></ul><ul><li>--The code below is equivalent to the cursor </li></ul><ul><li>select title_id, type, convert(money, price*1.08) </li></ul><ul><li>from titles </li></ul><ul><li>where type = ‘mod_cook’ </li></ul>
  18. 18. Data Modification <ul><li>You can delete a row based on the cursor position </li></ul><ul><li>Declare the cursor for update </li></ul><ul><li>declare mycursor … for update </li></ul><ul><li>open mycursor </li></ul><ul><li>fetch mycursor </li></ul><ul><li>delete … where current of mycursor </li></ul>
  19. 19. Data Modification <ul><li>For data modification based on cursor position, the table requires a unique index </li></ul><ul><li>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 </li></ul>
  20. 20. Data Modification <ul><li>You can also update data based on cursor position </li></ul><ul><li>declare mycursor … for update </li></ul><ul><li>open mycursor </li></ul><ul><li>fetch mycursor </li></ul><ul><li>update … where current of mycursor </li></ul>
  21. 21. Data Modification <ul><li>An update does not move the cursor position </li></ul><ul><li>The same row can be updated more than once until the next fetch is performed </li></ul><ul><li>You can update a multi-table view or joined tables only if the update is being performed on one table </li></ul>
  22. 22. Cautions <ul><li>Always specify for read only or for update </li></ul><ul><li>Do not leave cursors open for a long time </li></ul><ul><li>If the same operation is performed on every row in the result set, do not use a cursor </li></ul><ul><li>Always close and deallocate your cursors </li></ul>
  23. 23. Unit 13 Review <ul><li>Cursor allow you to perform an operation one row at a time </li></ul><ul><li>Cursors can have two modes: </li></ul><ul><ul><li>for read only </li></ul></ul><ul><ul><li>for update </li></ul></ul><ul><li>The steps to using a cursor are: </li></ul><ul><ul><li>declare </li></ul></ul><ul><ul><li>open </li></ul></ul><ul><ul><li>fetch </li></ul></ul><ul><ul><li>close </li></ul></ul><ul><ul><li>deallocate </li></ul></ul><ul><li>@@rowcount and @@sqlstatus or @@fetch_status will give you information about your fetch operations </li></ul><ul><li>For more flexibility, you can fetch data into variables </li></ul><ul><li>You can delete or update data by using the where current of cursor_name clause </li></ul><ul><li>Always close and deallocate your cursors </li></ul>
  24. 24. Unit 13 Exercises <ul><li>Time allotted for exercises is 1/2 hour </li></ul>