Database> SQL Server> cyclic cursor reading example

Source: Internet
Author: User

Table data:

ID name

1

2 B

3 C

 

Stored Procedure:

 

Set ansi_nulls on
Set quoted_identifier on
Go

Alter procedure [DBO]. [test1]
-- @ ID varchar (50) Output
As

Declare @ ID varchar (50) -- set the intermediate variable
Declare vari_cursor cursor scroll for select ID from IBS -- declares the cursor and points to the query result. The local parameter indicates that the cursor is valid only for this stored procedure.
Open vari_cursor -- open the cursor
Fetch next from vari_cursor -- extract data in the cursor
While (@ fetch_status = 0) -- sets the loop. 0 indicates that the cursor is successfully opened,-1 indicates that the statement fails, and-2 indicates that the extracted row does not exist.
Begin
Fetch next from vari_cursor into @ ID -- put the cursor value into a temporary variable
End

Close vari_cursor -- close the cursor
Deallocate vari_cursor -- release the cursor memory

Print @ ID -- output the value of the Temporary Variable

Note that the temporary variable @ ID in the database is strict and cannot be case-insensitive.

 

 

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.