jueves, 1 de diciembre de 2011

Convertir columnas de tabla SQL SERVER en variables de VB.NET

Este utilitario en sql sirve de mucho si se tiene una tabla de SQL SERVER con demasiadas columnas y se desea convertir cada columna en una variable de VB.NET.

SELECT 'DIM ' + t0.name + ' AS ' + UPPER(
CASE
WHEN t2.name IN ('text', 'varchar', 'char') THEN 'String'
WHEN t2.name IN ('ntext', 'nvarchar', 'nchar') THEN 'String'
WHEN t2.name IN ('tinyint', 'smallint', 'int', 'bigint') THEN 'Integer'
WHEN t2.name IN ('bit') THEN 'Boolean'
WHEN t2.name IN ('decimal', 'numeric', 'money', 'smallmoney') THEN 'Double'
WHEN t2.name IN ('float', 'real') THEN 'Double'
WHEN t2.name IN ('smalldatetime', 'datetime') THEN 'Date'
WHEN t2.name IN ('binary', 'varbinary', 'image') THEN 'String'
ELSE 'String'
END) + ' = ' +
CASE
WHEN t2.name IN ('text', 'varchar', 'char') THEN '""'
WHEN t2.name IN ('ntext', 'nvarchar', 'nchar') THEN '""'
WHEN t2.name IN ('tinyint', 'smallint', 'int', 'bigint') THEN '0'
WHEN t2.name IN ('bit') THEN 'False'
WHEN t2.name IN ('decimal', 'numeric', 'money', 'smallmoney') THEN '0.0'
WHEN t2.name IN ('float', 'real') THEN '0.0'
WHEN t2.name IN ('smalldatetime', 'datetime') THEN 'Date.Now'
WHEN t2.name IN ('binary', 'varbinary', 'image') THEN '""'
ELSE '""'
END
FROM sys.columns t0
INNER JOIN sys.tables t1 ON t1.object_id = t0.object_id
INNER JOIN sys.types t2 ON t2.system_type_id = t0.system_type_id
WHERE t1.name = 'TABLE'
ORDER BY t0.column_id

Esta consulta le mostrara todas las columnas de la tabla convertidas en definiciones de variables para VB.NET.

DIM COLUMNA1 AS INTEGER = 0
DIM COLUMNA1 AS STRING = ""
DIM COLUMNA3 AS BOOLEAN = False
DIM COLUMNA4 AS DATE = Date.Now
DIM COLUMNA5 AS DOUBLE = 0.0

(5 row(s) affected)

No hay comentarios: