The problem I am facing is that I cannot use the output of the first query I am running :select TABLE_NAME , COLUMN_NAME, DATA_TYPE, NUMERIC_PRECISIONfrom INFORMATION_SCHEMA.COLUMNS
in a second query, I want to use the COLUMN_NAME and TABLE_NAME values to build a new query that does the following:select COLUMN_NAME from TABLE_NAME
I tried creating a cursor with the first query and then going through it and building a new dynamic SQL query but it's a mess and doesn't work :(DECLARE @table VARCHAR(50), @column VARCHAR(50), @type VARCHAR(50), @precision VARCHAR(50), @pk VARCHAR(50)DECLARE result CURSOR FORselect I.TABLE_NAME , I.COLUMN_NAME, I.DATA_TYPE, I.NUMERIC_PRECISION, C.CONSTRAINT_NAMEfrom INFORMATION_SCHEMA.COLUMNS as I, INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE as Cwhere C.CONSTRAINT_NAME like 'PK_%' and I.TABLE_NAME = C.TABLE_NAMEOPEN resultFETCH result INTO @table, @column, @type, @precision, @pkWHILE @@FETCH_STATUS = 0BEGINEXECUTE ('SELECT ' + @column + ' FROM ' +@table)FETCH result INTO @table, @column, @type, @precision, @pkEndCLOSE resultDEALLOCATE result
any advice please ?