MSSQL Codex a MSSQL cursor

Source: Internet
Author: User

Unlike Oracle, there is no implicit cursor in MSSQL, and there are no%type and%rowtype keywords that update the meaning of variables in real time based on database fields, and MSSQL cursors are similar to Oracle's display cursors and need to be manually shut down.

After a cursor is defined, a cursor loop is made within a predefined variable, rather than a result set such as Oracle. As follows:

DeclareNexcursor  for Select [user_name],[Birthdate]  fromT_user_info--you need to define a variable when accessing a row of data in the cursor, which corresponds to the field one by one in the table.    Declare @name nvarchar( -)    Declare @birthdate datetime        --Open CursorOpenNEX--data in a variable result set via a cursor NEX loopFetchNex into @name,@birthdate while(@ @FETCH_STATUS=0)begin    --do some of the work here    Print('Name:'+@name+'    '+'Date:'+Convert(nvarchar( -),@birthdate))    --print (convert (nvarchar), @birthdate)    --print (@birthdate)    --search has been carried out. If @ @FETCH_STATUS is always 0, it means that it always has data and can be recycled.    FetchNex into @name,@birthdateEnd--read end, close cursorCloseNEX--when you close the cursor and then release the cursor, Oracle does not have this stepdeallocateNex

This is a cursor of the demo, you can copy it and then change the variable, field, table name, then do the operation, but the cursor will have some effect on performance, so use the time to use reasonable

MSSQL Codex a MSSQL 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.