T-SQL usage 3 (cursor and fetch)

Source: Internet
Author: User
/In general, when we use the select statements, they are all for a row of records,
If you want to read a multi-row record (that is, a record set) in the query analyzer, you need to use a cursor or a while loop.
/
The following is an excerpt from
Http://www.cnblogs.com/aotian56/archive/2007/07/13/817281.html
/
Cursor type:
1. Static cursor (does not detect changes in data rows)
2. Dynamic Cursor (reflecting changes to all data rows)
3. Forward cursor only (scroll is not supported)
4. Key set cursor (can reflect modifications, but cannot accurately reflect insertion or deletion)

Order of cursor usage:
1. Define a cursor
2. Open the cursor
3. Use a cursor
4. Close the cursor
5. Release cursor

Transact-SQL:
Declare cursor name cursor [local | Global] [forward_only | scroll] [static | keyset | dynamic] [read_only | scroll_locks]
For selet statement [for update [of column name [, column name]
Note: Local local cursor global Global cursor
Forward_only only scroll forward scroll
Static keyset
Read_only read-only scroll_locks lock the current row of the cursor

Obtain cursor data
Fetch [[next | prior | first | last |
Absolute {n | @ nvar | relative {n | @ nvar}]
From] cursor name [into variable]
Note:
Next next line prior last line first line
Last last line absolute N
Line n starting from the current position of relative n
The into variable assigns values of fields in the current row to the variable.

Cursor status variable:
@ Fetch_status cursor status
0 success-1 failure-2 loss
@ Cursor_rows the number of rows in the result set in the cursor
N rows-1 cursor is a dynamic 0 empty set cursor
The current row of the Operation cursor:
Current of cursor name

In the following example, the test is successful on SQL Server 2000.

Use Pubs
Go

Declare   @ Auid   Char ( 12 ), @ Aulname   Varchar ( 20 ), @ Aufname   Varchar ( 20 ), @ St   Char ( 2 ), @ Auinfo   Varchar ( 50 )
Declare Auth_cur Cursor   For
Select Au_id, au_lname, au_fname, state
From Authors

OpenAuth_cur

Fetch   Next   From Auth_cur Into   @ Auid , @ Aulname , @ Aufname , @ St
While ( @ Fetch_status = 0 )
Begin
Print   ' Author No: ' + @ Auid
Print   ' Author name: ' + @ Aulname + ' , ' + @ Aufname
Print   ' Region: ' + @ St
Print   ' -------------------------- '
Fetch   Next   From Auth_cur Into   @ Auid , @ Aulname , @ Aufname , @ St
End

CloseAuth_cur
DeallocateAuth_cur

Transact-SQL cursors are mainly used in Stored Procedures, triggers, and transact-SQL scripts. They allow the content of the result set to be used in other Transact-SQL statements.
/* Another example
From: http://www.cnblogs.com/BillChen/archive/2008/08/21/690180.html
The original example is used to explain how to use while to replace the cursor and its advantages. Here, we only extract the example of using the cursor.
*/
Declare   @ Tbtargetpermissions Table (Targetpermissionid Uniqueidentifier   Not   Null   Primary   Key )
Insert   Into   @ Tbtargetpermissions  
Select   [ Targetpermissionid ]  
From   [ Ps_rolepermissions ]  
Where   [ Targetid ]   =   @ Targetid   And   [ Roleid ]   =   @ Roleid

Declare   @ Targetpermissionid   Uniqueidentifier ;

-- Define a cursor
Declare Targetpermissions_byroleid_cursor Cursor   For  
Select   [ Targetpermissionid ]   From   @ Tbtargetpermissions  

-- Open cursor
Open Targetpermissions_byroleid_cursor

-- Read the first record of the cursor
Fetch   Next   From Targetpermissions_byroleid_cursor Into   @ Targetpermissionid

-- Check the value of @ fetch_status for cyclic reading.
While   @ Fetch_status   =   0
Begin
Exec Ps_targetpermissionentity_select @ Targetpermissionid ;

Fetch   Next   From Targetpermissions_byroleid_cursor Into   @ Targetpermissionid ;
End

-- Close cursor
Close Targetpermissions_byroleid_cursor
Deallocate Targetpermissions_byroleid_cursor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.