SQL Server Cursor instance demo had to look

Source: Internet
Author: User
Tags end sql variable

The following article mainly describes the SQL Server cursor instance, if you encounter a SQL Server cursor instance in the actual operation, but you do not know how to apply correctly to it, then the following article to you must be a mentor, hope will give you some help in this respect.

SQL Server Cursor instance:

 
 
  1. Declare Mycusror Cursor Scroll
  2. For a Select * from Master_goods ORDER by Goodsid
  3. Open MyCursor
  4. Fetch Next from MyCursor
  5. Into @GoodsCode, @GoodsName
  6. while (@ @Fetch_Status = 0)
  7. Begin
  8. Begin
  9. Select @GoodsCode = Convert (Char (), @GoodsCode)
  10. Select @GoodsName = Convert (Char (), @GoodsName)
  11. PRINT @GoodsCode + ': ' + @GoodsName
  12. End
  13. Fetch Next from MyCursor
  14. Into @GoodsCode, @GoodsName
  15. End
  16. Close MyCursor
  17. Deallocate mycursor

The data method for modifying the current cursor is as follows:

 
  
  
  1. UpDate master_goods Set goodsname = ' php100 ' Where current of MyCursor

The method for deleting the current cursor row data is as follows:

 
  
  
  1. Delete from Master_goods Where Current of MyCursor

The Select @ @CURSOR_ROWS can get the number of rows of data that exist in the current cursor. Note: This variable is a global variable on a connection and therefore corresponds to the last open cursor only.

Open cursor

Global cursors: Open global MyCursor

Local cursors: Open mycursor

The above is a description of the SQL Server cursor instance, and hope you get something.




Related Article

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.