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