• 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...

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001