The use of the SQL Midstream label

Source: Internet
Author: User
Tags rowcount

In general, when we use select these query statements, it is for a row of records,
If you want to read multiple rows of records (that is, recordsets) in Query Analyzer, you need to use loops such as cursors or while


Type of cursor:
1, static cursors (do not detect changes in data rows)
2. Dynamic cursor (reflects all data row changes)
3. Forward-only cursors (does not support scrolling)
4, keyset cursor (can reflect the changes, but can not accurately reflect the insertion, deletion)

Cursor Use order:
1. Defining cursors
2. Open cursor
3. Using Cursors
4. Close the cursor
5. Releasing the cursor

Transact-SQL:DeclareCursor namecursor [LOCAL | GLOBAL][Forward_only | SCROLL][STATIC | KEYSET | DYNAMIC] [Read_Only | Scroll_locks]     forSelet statements[for update[of column name [, column name]] Note: Local local cursor global global cursor forward_only only forward SCROLL scrolling static KEYSET keyset dynamic READ_ONLY Read Only SC Roll_locks Lock cursor The current row gets the data for the cursorFETCH [[NEXT | PRIOR | First |   Last | absolute{N | @nvar | RELATIVE {n | @nvar}]   from] Cursor Name[into variable]Note:NEXTThe next line PRIOR The first row, the last line ABSOLUTE n the nth row RELATIVE n the nth row starting at the current position intovariable assigns the value of each field in the current row to the variable cursor state variable:@ @fetch_statusCursor State0Success-1Failed-2lost@ @cursor_rowsNumber of rows in the result set in the cursor n rows-1Cursors are dynamic0Empty set cursors manipulate the current line of the cursor: Current  ofCursor name

Example 1: Iterating through a query's dataset with a cursor
 UsepubsGoDeclare @auid Char( A),@aulname varchar( -),@aufname varchar( -),@st Char(2),@auinfo varchar( -)DeclareAuth_curcursor  forSelectau_id, au_lname, au_fname, state fromauthorsOpenAuth_curFetch Next  fromAuth_cur into @auid,@aulname,@aufname,@st while(@ @fetch_status=0)  begin    Print 'Author Number:'+@auid    Print 'Author's name:'+@aulname+','+@aufname    Print 'state of the country:'+@st    Print '--------------------------'    Fetch Next  fromAuth_cur into @auid,@aulname,@aufname,@st  EndCloseAuth_curdeallocateAuth_cur

Example 2: Modifying and deleting data using cursors
DeclareAuth_curcursorScroll forSelectau_id, au_lname, au_fname, state fromAuthors for Update  ofau_lnameOpenAuth_curDeclare @rowcount intSet @rowcount = 6FetchAbsolute@rowcount  fromAuth_cur--set the row identified by the variable @rowcount to the current row--The following 1 lines are used to modify operations using cursorsUpdateAuthorsSetau_lname='Zhang' where  Current  ofAuth_cur--to modify the current row in a cursor--The following 1 lines are used to delete operations using cursorsDelete  fromAuthorswhere  Current  ofAuth_cur

The use of the SQL Midstream label

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.