I need the same from time to time. Here's a small script I put together. It's a little rude, and I will not trust this with my life, but it works well enough for my business. These are not script keys, but for my script this is not necessary. I'm on SQL 2012, though, so I'm not quite sure that this will work the same way as on SQL 2008. I have not tested it for some of the more "exotic" types, such as geometry , geography and friends, since I never did not have to use them.
declare @tablename nvarchar(50)='Users', @schemaname nvarchar(50)='dbo', @sql nvarchar(max)=N''; select @sql += N',' + NCHAR(13) + NCHAR(10) + NCHAR(9) + N'[' + c.COLUMN_NAME + N'] [' + DATA_TYPE + N']' + case when c.CHARACTER_MAXIMUM_LENGTH is not null then N'(' + case c.CHARACTER_MAXIMUM_LENGTH when -1 then 'max' else cast(c.CHARACTER_MAXIMUM_LENGTH as nvarchar(10)) end + N')' else N'' end + case when c.DATA_TYPE = N'numeric' then N'('+CAST(NUMERIC_PRECISION as nvarchar(10))+N', '+CAST(NUMERIC_SCALE as nvarchar(10))+N')' else N'' end + case when c.is_nullable <> N'NO' then N' NULL' else N' NOT NULL'end from INFORMATION_SCHEMA.COLUMNS c where TABLE_NAME = @tablename AND TABLE_SCHEMA = @schemaname order by ORDINAL_POSITION; set @sql = stuff(@sql, 1, 1, N'CREATE TYPE [' + @schemaname + N'].[tab_' + @tablename + N'] AS TABLE(') + nchar(13) + nchar(10) + ')' + nchar(13) + nchar(10) + 'GO'; set @sql += nchar(13) + nchar(10) + '--GRANT EXEC ON TYPE::[' + @schemaname + N'].[tab_' + @tablename + N'] TO [User];' + nchar(13) + nchar(10) + '--GO'; print @sql
takrl source share