Your SlideShare is downloading. ×
0
Module07
Module07
Module07
Module07
Module07
Module07
Module07
Module07
Module07
Module07
Module07
Module07
Module07
Module07
Module07
Module07
Module07
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Module07

739

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
739
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • 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
  • Transcript

    • 1. 7.1 : Cursors SQL Server 2005
    • 2. <ul><li>What is a cursor </li></ul><ul><li>Types of cursors </li></ul><ul><li>Working with cursors </li></ul><ul><li>Declare cursor </li></ul><ul><li>Open cursor </li></ul><ul><li>Fetch cursor </li></ul><ul><li>Close cursor </li></ul>Contents
    • 3. Objectives <ul><li>At the end of this presentation you should be able to: </li></ul><ul><li>Understand the concept of cursor </li></ul><ul><li>Understand why cursor is required in a database </li></ul><ul><li>Understand the different types of cursors </li></ul><ul><li>Working with cursor </li></ul>
    • 4. What is a Cursor <ul><li>A cursor is a database object that helps in accessing and manipulating row by row data in a given result set. </li></ul><ul><li>Cursor serves as a mechanism for an application to operate on a single row or a small set of rows . </li></ul>
    • 5. What is a Cursor (Continued) <ul><li>It allows specific rows to be retrieved from result set </li></ul><ul><li>It allows the current row in result set to be modified </li></ul><ul><li>Helps to navigate from the current row in the result set to a different row </li></ul><ul><li>Allows data to be modified by other users to be visible in the result set. </li></ul>
    • 6. Types of Cursors <ul><li>Cursors can be classified as follows: </li></ul><ul><li>Static </li></ul><ul><li>Forward_only </li></ul><ul><li>Keyset driven </li></ul><ul><li>Dynamic </li></ul>
    • 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
    • 9. To work with Cursors <ul><li>The following process defines the working of a cursor in SQL Server: </li></ul><ul><ul><li>The cursor needs to be defined and its attributes need to be set. </li></ul></ul><ul><ul><li>The cursor needs to be opened </li></ul></ul><ul><ul><li>The required rows need to be fetched from the cursor </li></ul></ul><ul><ul><li>The data in the current row of the cursor can be modified, if required </li></ul></ul>
    • 10. To Work with Cursors (Continued) <ul><li>User can define a cursor and its characteristics set by using the DECLARE CURSOR statement. </li></ul><ul><li>To Declare Cursors : </li></ul>Syntax DECLARE cursor_name[INSENSITIVE ] [SCROLL] CURSOR FOR{select_statement | prepared statement} [FOR {READ ONLY | UPDATE[OF column_list]}]
    • 11. To Work with Cursors (Continued) <ul><li>To Open Cursor : User can open a cursor using the open statement. </li></ul><ul><li>To Fetch Data : Once a cursor has been opened, you can retrieve a specific row from the result set of the cursor.SQL Server provides the FETCH statement to accomplish the task. </li></ul>Syntax OPEN Cursor_name
    • 12. To Work with Cursors (Continued) <ul><li>To Fetch Data : </li></ul><ul><li>Syntax </li></ul><ul><li>To Close cursor : User must close a cursor in order to release the resources held by it. A cursor can be closed with CLOSE statement </li></ul><ul><li>Deallocate Cursor : Will remove the cursor reference. When the cursor is deallocated and if it is the last reference, the structures of the cursor are released. </li></ul>FETCH [[NEXT|PRIOR|FIRST|LAST]FROM]cursor_name [using Descriptor :sqlda_struct |INTO : hvar[…]] CLOSE cursor_name DEALLOCATE cursor_name
    • 13. Example 1 - Cursor <ul><li>declare @author_id char(20) </li></ul><ul><li>declare c2 cursor forward_only for </li></ul><ul><li>select au_id from authors </li></ul><ul><li>open c2 </li></ul><ul><li>fetch next from c2 into @author_id </li></ul><ul><li>while @@fetch_status = 0 </li></ul><ul><li>begin </li></ul><ul><li>print @author_id </li></ul><ul><li>fetch next from c2 </li></ul><ul><li>into @author_id </li></ul><ul><li>end </li></ul><ul><li>close c2 </li></ul><ul><li>deallocate c2 </li></ul>
    • 14. Example 2 - Cursor <ul><li>Declare example cursor keyset </li></ul><ul><li>For select * from report </li></ul><ul><li>Open example </li></ul><ul><li>Fetch last from example </li></ul><ul><li>Delete from report where id=2 </li></ul><ul><li>Fetch relative -1 from example </li></ul><ul><li>Select @@fetch_status </li></ul><ul><li>Close example </li></ul><ul><li>Deallocate example </li></ul>
    • 15. Activity Time(60 minutes) <ul><li>Activity: 7.1 </li></ul><ul><li>Create a cursor to retrieve and display the Publisher_ID and Publisher_Name from the Publisher table. </li></ul><ul><li>Activity 7.2 </li></ul><ul><li>Create a Cursor which retrieves the Title, Type and Price from the Title table and displays the records one after the other in the following format. </li></ul><ul><li>----------------------------------------------------------------------- </li></ul><ul><li>Title : The Busy Executives Database Guide </li></ul><ul><li>Title Type : Business </li></ul><ul><li>Price : 19.99 </li></ul><ul><li>----------------------------------------------------------------------- </li></ul>
    • 16. Activity Time(60 minutes) continued <ul><li>Activity: 7.3 </li></ul><ul><li>Create a cursor to update the Quantity of the Items in the Items Table for the Orders Ordered in the OrderDetail table. </li></ul><ul><li>Hint: Use Nested Cursor (Cursor within a Cursor) </li></ul>
    • 17. Questions & Comments

    ×