no insults, just help with the logical rules of thumb for procedures and functions:
avoid loops whenever possible: that's cursor, while loops whatever;
like everything in SQL, there's more than one way to do anything, and anything set based is going to be faster...i try to think that SQL can handle a SET of Data in exactly the same amount of time it would take to process a single row.
kind of like moving one apple vs a box of apples...handing it to the next process is the same amount of time.
my examples are probably pretty good, but i'm always ready for a better technique than i'm throwing out there.;
Lowell