Create loop through the recordset in stored procedure can be confusing for some people, searching the internet I saw that there are many example where using a rowcount and a increment variable through the records a table. Something like this:
But from the 2005 version of sql server you can cycle through the values of the recordset using a FETCH NEXT FROM MyCursor INTO @ col. That controls the cursor query. In this example I show below an example of a stored procedure that performs a query in a table of customers and returns the fields id and age of customers, and then makes a loop through the recordset and the value of the variable age checks if the client has more than 60 years. If the client has more than 60 years, the stored procedure adds a record in table bonus passing the customer id.

0 comentários:
Postar um comentário