Below is an excerpt from an article from SQL-Server-Performance.com. They sound like good ideas. However, they give no examples of how to transform a cursor using these methods. I suppose I should use my imagination.
Is there someone who can illustrate an example of how to use each method below. I guess I am still stuck in the cursor mentality, in that I don't believe that you can always avoid cursors. But then, I am no SQL genius.
Here's an example. Let's say you have a table and you want to update a datetime field, but the value by which to update will be different in each case. In some cases, you want to add 7 days to the datetime field. In other cases, 1, 2, 3 days... and it's dependent on other values in the record. How could you use the techniques below to do this?
I do appreciate everyone's advice NOT to use cursors. But how does one get around them, for example, in the case above?
Thanks
Is there someone who can illustrate an example of how to use each method below. I guess I am still stuck in the cursor mentality, in that I don't believe that you can always avoid cursors. But then, I am no SQL genius.
Here's an example. Let's say you have a table and you want to update a datetime field, but the value by which to update will be different in each case. In some cases, you want to add 7 days to the datetime field. In other cases, 1, 2, 3 days... and it's dependent on other values in the record. How could you use the techniques below to do this?
I do appreciate everyone's advice NOT to use cursors. But how does one get around them, for example, in the case above?
Thanks
If possible, avoid using SQL Server cursors. They generally use a lot of SQL Server resources and reduce the performance and scalability of your applications. If you need to perform row-by-row operations, try to find another method to perform the task.
Here are some alternatives to using a cursor:
Use WHILE LOOPS
Use temp tables
Use derived tables
Use correlated sub-queries
Use the CASE statement
Perform multiple queries