Please don't double post.
I inlcuded a better script for handling this
in your original post that does not include any cursors or loops.
P.S. if you search online for a way to do something in SQL and you find a solution that inlcudes a cursor or loop - I'd suggest that you keep searching...
-- Itzik Ben-Gan 2001