• You are probably under a bit of pressure here and consequently not communicating your problem as well as you could?

    I can't see how a loop can help you here as you are not even testing for the same conditions... unless you're talking about nesting your While loops in which case I can't see how that will be any more elegant than what you currently have.

    You may need to provide a little more context around the while.. loop approach...

    As far as optimising the procedure is concerned, you'll need to provide a lot more info about the underlying database objects the procedure interacts with such as other procedures, tables and available indexes, execution plans and the like to get any useful help although having not hinted to performance issues as such you may be really wanting to refactor the procedure more than anything else.

    Take a step back and try to clarify the requirements as you would to a "new joiner" in your company and you might get better help bearing in mind that your procedure contains business logic no one outside your organisation/team has any knowledge about.