• This procedure can be changed into set-based code. I just don't want to risk posting an incorrect solution and getting blamed for that as I don't have any sample data to test on or more details on what the code is meant to do.

    For a minor change that might help a bit, you could wrap everything inside a single transaction. That will also help you to correct errors that you might get if the procedure fails without completing.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2