Your SlideShare is downloading. ×
Cursor
Cursor
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

Cursor

204

Published on

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

  • Be the first to like this

No Downloads
Views
Total Views
204
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
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

Transcript

  • 1. Cursors in SQL proceduresIn SQL procedures, a cursor make it possible to define a result set (aset of data rows) and perform complex logic on a row by row basis. Byusing the same mechanics, an SQL procedure can also define a result setand return it directly to the caller of the SQL procedure or to a clientapplication.A cursor can be viewed as a pointer to one row in a set of rows. Thecursor can only reference one row at a time, but can move to other rowsof the result set as needed.To use cursors in SQL procedures, you need to do the following:1.Declare a cursor that defines a result set.2.Open the cursor to establish the result set.3.Fetch the data into local variables as needed from the cursor, one rowat a time.4.Close the cursor when doneTo work with cursors you must use the following SQL statements:•DECLARE CURSOR•OPEN•FETCH•CLOSEThe following example demonstrates the basic use of a read-only cursorwithin an SQL procedure: CREATE PROCEDURE sum_salaries(OUT sum INTEGER) LANGUAGE SQL BEGIN DECLARE SQLSTATE CHAR(5) DEFAULT 00000; DECLARE p_sum INTEGER; DECLARE p_sal INTEGER; DECLARE c CURSOR FOR SELECT SALARY FROM EMPLOYEE; SET p_sum = 0; OPEN c; FETCH FROM c INTO p_sal; WHILE(SQLSTATE = 00000) DO SET p_sum = p_sum + p_sal; FETCH FROM c INTO p_sal; END WHILE; CLOSE c; SET sum = p_sum;
  • 2. END;;

×