This is a very interesting quandary indeed. I am sure this can be done using a little less code but here is what I came up with:
SET NOCOUNT ON
GO
/***********************************************************************
1) Create...
"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