• sani.bozic - Thursday, July 12, 2018 6:20 AM

    How did I miss that with all the reading about cursors... Thank you very much for the essential info about cursors.
    Is there an elegant solution or do I have to rewrite the whole code twice - before and after loop?

    I'm not aware of any way to avoid duplicating the code.   You have to initialize @@FETCH_STATUS before you can check it's value, and the only way to do that is via FETCH NEXT.   You could create a separate stored procedure whose parameters are entirely those defined for the cursor to deliver values into, and then execute that once before the loop, and then once inside the loop, and that would minimize code duplication, but that's a long way to go for not much savings, plus the overhead of another 2 procedure calls, or however many times the loop runs....

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)