190 likes | 310 Views
What Will Be the Output Of. Select * from Student. I want to Display the LAST record I may require to move to the First record I want to go to record no 5 I want to go the next record … Previous Record. I am Sorry ????. Select Statement . Select……….
E N D
What Will Be the Output Of Select * from Student
I want to Display the LAST record I may require to move to the First record I want to go to record no 5 I want to go the next record … Previous Record..
I am Sorry ???? Select Statement
Select……… • Operations in a relational database act on a complete set of rows. • The set of rows returned by a SELECT statement consists of all the rows that satisfy the conditions in the WHERE clause of the statement. • This complete set of rows returned by the statement is known as the result set
I am Sorry ???? Select Statement I am Comfortable cursor
Cursor • Is a database object who can navigate the records one by one not as a whole. • Using cursor we can move forward, backward, jump some records ahead, position the record pointer at any position. • We can able to update / Delete current records.
Cursors extend result processing by: • Allowing positioning at specific rows of the result set. • Retrieving one row or block of rows from the current position in the result set. • Supporting data modifications to the rows at the current position in the result set. • Providing Transact-SQL statements in scripts, stored procedures, and triggers access to the data in a result set.
Steps to Declare & use • Declare the Cursor • Open the cursor • Fetch the Cursor • Close the Cursor
Declaring DECLARE cursor_name CURSOR [ FORWARD_ONLY | SCROLL ] [ STATIC |DYNAMIC ] FOR select_statement [ FOR UPDATE [ OF column_name [ ,...n ] ] ]
Declaring a Forward readonly Cursor DECLARE frd_cur CURSOR FORWARD_ONLY STATIC FOR Select * from student Declaring a Forward Updateable Cursor DECLARE frd_cur CURSOR FORWARD_ONLY Dynamic FOR Select * from student
Opening The Cursor Open <cursor_name> This will Open the cursor for use.
Fetching FETCH [ [ NEXT | PRIOR | FIRST | LAST | ABSOLUTE { n } | RELATIVE { n } ] FROM ] <cursor_name >
Closing the Cursor Close <cur_name> This will Close the Cursor.
Example (Forward Readonly) Declare cur1 Cursor for Select * from stud Open cur1 Fetch next from cur1 Close cur1
Example (Forward Dynamic) 1. Declare cur1 Cursor dynamic for Select * from stud for update 2. Open cur1 3. Fetch next from cur1
Deleting the Current Record using Cursor Delete from stud where current of cur1
Example (Scroll Readonly) Declare cur1 Cursor Scroll for Select * from stud Open cur1 Fetch absolute 5 from cur1 Fetch relative 5 from cur1 Close cur1