Aquí hay uno, si sólo puede SELECT ... INTO #Temp
(recuerda #Temp
se alcance la sesión en el peor) con un poco robado de https://stackoverflow.com/a/14328779/162273:
SELECT
c.name AS UsefulRawName,
',' + c.name + ' ' + UPPER(t.name) +
CASE
WHEN t.name IN ('char', 'nchar', 'varchar', 'nvarchar') THEN '(' + CAST(c.max_length AS VARCHAR(3)) + ')'
WHEN t.name IN ('decimal', 'numeric') THEN '(' + CAST(c.[precision] AS VARCHAR(3)) + ', ' + CAST(c.[scale] AS VARCHAR(3)) + ')'
ELSE '' END + CASE WHEN c.Is_Nullable = 1 THEN ' NULL' ELSE ' NOT NULL' END AS SQLColumnType,
'public ' +
CASE
WHEN t.name IN ('varchar', 'nvarchar', 'text', 'ntext', 'char', 'nchar', 'xml', 'sysname') THEN 'string'
WHEN t.name IN ('binary', 'varbinary', 'image') THEN 'byte[]' + CASE WHEN c.Is_Nullable = 1 THEN '?' ELSE '' END
WHEN t.name IN ('uniqueidentifier') THEN 'Guid' + CASE WHEN c.Is_Nullable = 1 THEN '?' ELSE '' END
WHEN t.name IN ('datetimeoffset') THEN 'DateTimeOffset' + CASE WHEN c.Is_Nullable = 1 THEN '?' ELSE '' END
WHEN t.name IN ('date', 'time', 'datetime2', 'smalldatetime', 'datetime') THEN 'DateTime' + CASE WHEN c.Is_Nullable = 1 THEN '?' ELSE '' END
WHEN t.name IN ('numeric', 'decimal', 'real', 'money', 'smallmoney') THEN 'decimal' + CASE WHEN c.Is_Nullable = 1 THEN '?' ELSE '' END
WHEN t.name IN ('float') THEN 'float' + CASE WHEN c.Is_Nullable = 1 THEN '?' ELSE '' END
WHEN t.name IN ('tinyint', 'smallint') THEN 'short' + CASE WHEN c.Is_Nullable = 1 THEN '?' ELSE '' END
WHEN t.name IN ('int') THEN 'int' + CASE WHEN c.Is_Nullable = 1 THEN '?' ELSE '' END
WHEN t.name IN ('bit') THEN 'bool' + CASE WHEN c.Is_Nullable = 1 THEN '?' ELSE '' END
WHEN t.name IN ('bigint') THEN 'long' + CASE WHEN c.Is_Nullable = 1 THEN '?' ELSE '' END
WHEN t.name IN ('timestamp') THEN 'ulong'
ELSE 'object' END + ' ' + c.name + ' { get; set; }' AS CSColumnType,
c.name + ' = ' +
CASE
WHEN t.name IN ('varchar', 'nvarchar', 'text', 'ntext', 'char', 'nchar', 'xml', 'sysname') THEN 'reader["' + c.name + '"] as string,'
WHEN t.name IN ('binary', 'varbinary', 'image') THEN CASE
WHEN c.Is_Nullable = 1 THEN 'reader["' + c.name + '"] as byte[]?,'
ELSE '(byte[])reader["' + c.name + '"],' END
WHEN t.name IN ('uniqueidentifier') THEN CASE
WHEN c.Is_Nullable = 1 THEN 'reader["' + c.name + '"] as Guid?,'
ELSE '(Guid)reader["' + c.name + '"],' END
WHEN t.name IN ('datetimeoffset') THEN CASE
WHEN c.Is_Nullable = 1 THEN 'reader["' + c.name + '"] as DateTimeOffset?,'
ELSE '(DateTimeOffset)reader["' + c.name + '"],' END
WHEN t.name IN ('date', 'time', 'datetime2', 'smalldatetime', 'datetime') THEN CASE
WHEN c.Is_Nullable = 1 THEN 'reader["' + c.name + '"] as DateTime?,'
ELSE '(DateTime)reader["' + c.name + '"],' END
WHEN t.name IN ('numeric', 'decimal', 'real', 'money', 'smallmoney') THEN CASE
WHEN c.Is_Nullable = 1 THEN 'reader["' + c.name + '"] as decimal?,'
ELSE '(decimal)reader["' + c.name + '"],' END
WHEN t.name IN ('float') THEN CASE
WHEN c.Is_Nullable = 1 THEN 'reader["' + c.name + '"] as float?,'
ELSE '(float)reader["' + c.name + '"],' END
WHEN t.name IN ('tinyint', 'smallint') THEN CASE
WHEN c.Is_Nullable = 1 THEN 'reader["' + c.name + '"] as short?,'
ELSE '(short)reader["' + c.name + '"],' END
WHEN t.name IN ('int') THEN CASE
WHEN c.Is_Nullable = 1 THEN 'reader["' + c.name + '"] as int?,'
ELSE '(int)reader["' + c.name + '"],' END
WHEN t.name IN ('bit') THEN CASE
WHEN c.Is_Nullable = 1 THEN 'reader["' + c.name + '"] as bool?,'
ELSE '(bool)reader["' + c.name + '"],' END
WHEN t.name IN ('bigint') THEN CASE
WHEN c.Is_Nullable = 1 THEN 'reader["' + c.name + '"] as long?,'
ELSE '(long)reader["' + c.name + '"],' END
WHEN t.name IN ('timestamp') THEN '(ulong)reader["' + c.name + '"],'
ELSE 'reader["' + c.name + '"] == DBNull.Value ? null : reader["' + c.name + '"],' END AS ReaderStatements
FROM tempDb.sys.columns c
INNER JOIN sys.types AS t ON c.system_type_id = t.system_type_id AND t.system_type_id = t.user_type_id
WHERE [object_id] = OBJECT_ID('tempdb..#Temp')
ORDER BY column_id
Una persona más motivada podría hacer un mapeo más inteligente de los tipos a .Net equivalentes automáticamente, pero esto no es tan malo y mantiene el fragmento corto.
La conversión manual de estos tipos fue suficiente como para hacer que la escritura valiera la pena. No estoy seguro de que mis conversiones sean todas correctas.
Esto depende en gran medida de qué idioma/plataforma se utiliza para recuperar esos valores. –