Skip to content
Advertisement

Save SQL Query Results in Variable

I have the following SQL Query, but it is very repetitive and so I am wondering if this can be cleaned up by storing the results of a SQL Query in a variable.

What I have is (which works, can be tested here)

And as you could tell the following code is repeated twice, is there any way I can do this without repeating this code:

Advertisement

Answer

You can use CTE to avoid looping, maybe like this:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement