I went through a similar exercise not long ago...
I haven't actually run the code from this article, since I'm quite happy with mine
, but looked through it and have a couple of general comments/suggestions (more to do with the SQL than the auto-generation logic)
1) lose all the CHAR(10) + CHAR(13) stuff, and use embedded linefeeds in your constants (see my other reply). it'll make it much easier to read and maintain (once you can get your head around the fact that you can actually do it - it took me a while).
2) if you're using this on SQL 2005 or later, you can switch all your varchar(8000)s for varchar(MAX)s, and the 8000 character restriction will go.
3) you should probably declare your '@Tablename' as nvarchar(128) or sysname - which is what SQL stores them as. Even if 50 already seems extremely long for a table name...
4) nchars and nvarchars have a length that is half the 'datalength' that you get from sys.columns (I may have missed that in your code).
5) rather than use a multi-line CASE..WHEN..WHEN..WHEN construct, you could use an 'IN' statement instead e.g.: select case when 'a' in ('a','b','c') then 'yes' else 'no' end.
P.S. just for interest' sake, I'll attach the SQL 2005+ code for a view and udf that I use for generating the 'metadata' in my equivalent sproc. In my database, I've left the view there permanently, but there's no reason why it couldn't be used as a temp table-populating query inside a sproc.