DECLARE @S VARCHAR(2000)
SET @S=''
SELECT @S=@S+NAME+
+SPACE(20-LEN(NAME))+
CASE WHEN xtype=34 THEN 'image'
WHEN xtype=35 THEN 'text'
WHEN xtype=36 THEN 'uniqueidentifier'
WHEN xtype=48 THEN 'tinyint'
WHEN xtype=52 THEN 'smallint'
WHEN xtype=56 THEN 'int'
WHEN xtype=58 THEN 'smalldatetime'
WHEN xtype=59 THEN 'real'
WHEN xtype=60 THEN 'money'
WHEN xtype=61 THEN 'datetime'
WHEN xtype=62 THEN 'float'
WHEN xtype=98 THEN 'sql_variant'
WHEN xtype=99 THEN 'ntext'
WHEN xtype=104 THEN 'bit'
WHEN xtype=106 THEN 'decimal'
WHEN xtype=108 THEN 'numeric'
WHEN xtype=122 THEN 'smallmoney'
WHEN xtype=127 THEN 'bigint'
WHEN xtype=165 THEN 'varbinary'
WHEN xtype=167 THEN 'varchar'+'('+LTRIM(length)+')'
WHEN xtype=173 THEN 'binary'
WHEN xtype=175 THEN 'char'+'('+LTRIM(length)+')'
WHEN xtype=189 THEN 'timestamp'
WHEN xtype=231 THEN 'nvarchar'+'('+LTRIM(length)+')'
WHEN xtype=239 THEN 'nchar'+'('+LTRIM(length)+')'
WHEN xtype=241 THEN 'xml'
WHEN xtype=231 THEN 'sysname' END+','+CHAR(10)
FROM SYSCOLUMNS a,
(
select 3 as colorder,1 as id union select 2,2 union select 1,3
) b
WHERE a.id=OBJECT_ID('sysobjects') and a.colorder = b.colorder order by b.id
SET @S='CREATE TABLE #('+CHAR(13)+LEFT(@S,LEN(@S)-2)+CHAR(13)+')'
PRINT @S
/*
CREATE TABLE #(
xtype char(2),
id int,
name nvarchar(256)
)
*/
SET @S=''
SELECT @S=@S+NAME+
+SPACE(20-LEN(NAME))+
CASE WHEN xtype=34 THEN 'image'
WHEN xtype=35 THEN 'text'
WHEN xtype=36 THEN 'uniqueidentifier'
WHEN xtype=48 THEN 'tinyint'
WHEN xtype=52 THEN 'smallint'
WHEN xtype=56 THEN 'int'
WHEN xtype=58 THEN 'smalldatetime'
WHEN xtype=59 THEN 'real'
WHEN xtype=60 THEN 'money'
WHEN xtype=61 THEN 'datetime'
WHEN xtype=62 THEN 'float'
WHEN xtype=98 THEN 'sql_variant'
WHEN xtype=99 THEN 'ntext'
WHEN xtype=104 THEN 'bit'
WHEN xtype=106 THEN 'decimal'
WHEN xtype=108 THEN 'numeric'
WHEN xtype=122 THEN 'smallmoney'
WHEN xtype=127 THEN 'bigint'
WHEN xtype=165 THEN 'varbinary'
WHEN xtype=167 THEN 'varchar'+'('+LTRIM(length)+')'
WHEN xtype=173 THEN 'binary'
WHEN xtype=175 THEN 'char'+'('+LTRIM(length)+')'
WHEN xtype=189 THEN 'timestamp'
WHEN xtype=231 THEN 'nvarchar'+'('+LTRIM(length)+')'
WHEN xtype=239 THEN 'nchar'+'('+LTRIM(length)+')'
WHEN xtype=241 THEN 'xml'
WHEN xtype=231 THEN 'sysname' END+','+CHAR(10)
FROM SYSCOLUMNS a,
(
select 1 as colorder,1 as id union select 2,2 union select 3,3
) b
WHERE a.id=OBJECT_ID('sysobjects') and a.colorder = b.colorder order by b.id
SET @S='CREATE TABLE #('+CHAR(13)+LEFT(@S,LEN(@S)-2)+CHAR(13)+')'
PRINT @S
/*
CREATE TABLE #(
name nvarchar(256),
id int,
xtype char(2)
)
*/