• Waht are your errors?

    You can use the built-in debug tool for stored procedure in QA.

    Also, you may add some PRINT commands between your code and see whether or not you can get expected results in executing your script.