10
I do not know if this is a question that many ask, but I have always had it in mind almost always.
Usually, when I need to deliver a script corrective to a customer, used only to be executed once, I use a CURSOR
, perhaps because it is more readable to those who do not know much about TSQL and because it is, in a way, simpler to maintain (recursiveness can often "complicate").
But in development I try, whenever possible, to use CTE
because it is "what should be used", according to experts in this field.
So I question: CTE or CURSOR?
I know that CTE is supposed to offer more performance in relation to the CURSOR, but it will be enough to use it to the detriment of the other option?
The issue of recursion, as I mentioned above, can often complicate a simple process, and can even make it very difficult to maintain a script of various levels in a more complex scenario.
In CTE
it is not possible for us to understand exactly what is done in between, while with a CURSOR
we managed to put a PRINT
or SELECT
with important or useful information in a debug or validation of results.
In short, use a CURSOR
using temporary (for example), or even a cycle WHILE
, is so much worse than using a CTE?
Of course it must depend on the scenario, but therefore, where we should use one and the other?
I think it’s clearer that this is impossible ;)! Thanks @Maniero.
– João Martins