I want to dynamically generate variable declarations based on a table. I want to say that I want to declare table variables, each variable must be of the same type as its column. I do something similar, but the result only works if the type is not int
Select 'Declare @Doc' + COLUMN_NAME + ' '+DATA_TYPE+case(CHARACTER_MAXIMUM_LENGTH) when Null then ' ' else '(' +convert(varchar(12),CHARACTER_MAXIMUM_LENGTH) + ')' end
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'Documentos'
And my result is
(No column name)
NULL
Declare @DocSerie varchar(5)
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
Declare @DocImporteLetras varchar(255)
Declare @DocMotivos text(2147483647)
NULL
Declare @DocDocumentosReferencia varchar(255)
NULL
NULL
Declare @DocAuditoriaIPC varchar(40)
NULL
NULL
Declare @DocAuditoriaIPM varchar(40)
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
source
share