To be honest, the cursor has never been used since ms SQL Server was used for such a long time. Previously, similar functions were implemented using a WHILE loop and a temporary table. I wrote a reference to the online example exercise just now. Learn about the cursor concept and syntax.
In the following code example,
First, declare that you need to use variables in the cursor, that is, temporarily store data that processes fields.
2. Declare a cursor and SELECT the dataset to be processed.
3. Open the cursor (#8 lines of code ).
4. Use fetch next data from the cursor to assign values to the variable.
5. Loop @ FETCH_STATUS = 0.
6. In the loop block, you can process the first record logic. In this example, PRINT is used.
7. After the logic processing is completed, fetch next data is taken from the cursor again to assign a value to the variable. (Same as above 4 ).
8. Close the cursor.
9. Delete and release the cursor.
Execution result: