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.