DECLARE @temp_temp uniqueidentifier--temporary variable declare AAA cursor for select Id from A-------------------open A cursor The aaa--first queries the recirculation to prevent multiple cursors when @ @FETCH_STATUS =-1 cannot enter the next cursor loop FETCH next from the AAA into @temp_ The temp-------------------loops through the data while @ @FETCH_STATUS =0beginprint @temp_tempfetch next from AAA into @temp_ Tempend-----------------------------------close a cursor close AAA -----------------------------------Delete a cursor DEALLOCATE AAA
Nesting of cursors
DECLARE @temp_temp uniqueidentifier--temporary variable declare AAA cursor for select Id from A-------------------open A cursor The aaa--first queries the recirculation to prevent multiple cursors when @ @FETCH_STATUS =-1 cannot enter the next cursor loop FETCH next from the AAA into @temp_ Temp-------------------loop Fetch data while @ @FETCH_STATUS =0beginprint @temp_temp--=========================== cursor Nesting DECLARE BBB cursor FOR select Id from B-------------------Open the cursor opening bbb--first to query the recirculation, to prevent multiple cursors @ @FETCH_STATUS =-1 cannot enter the next cursor loop in the case of FETCH Next from the BBB into @temp_temp-------------------loop to fetch the data while @ @FETCH_STATUS =0beginprint @temp_tempfetch next from the BBB into @temp_tempend-----------------------------------Close cursors close bbb -----------------------------------Delete Cursors deallocate bbb--=========================== cursor nesting fetch NEXT from AAA to @temp_ Tempend-----------------------------------close a cursor close AAA -----------------------------------Delete a cursor DEALLOCATE AAA
Use of SQL Server cursors