Stick the following line after each bit of sql that you want to check.
select 'test a', @@rowcount, getdate()
The string indicates where it is in the code, you’ll want to change it to something a little more descriptive, the @@rowcount indicates how many rows were effected in the previous sql statement and the getdate() shows the time. Make sure you put this line in each while loop and if-then-else construct. Then run your stored proc using isql, passing the parameters that will test all the paths in your procedure.
If there’s tonnes of output then send the output to a file. When you’re sure the procedure is fine then remove these lines. You could have a test flag that turns this debugging on so you can initially test things – then you can turn this test output off when the system is running fine – then if someone discovers there’s a bug you can easily turn it back on.