My aim is to use the rows of an already existing table as the columns of a new table
I have a query in existence already called #columns whereby it outputs a list of the distinct column headings over a number of specific existing tables: SELECT DISTINCT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME LIKE '%201703%IMP' I now want to create a new table which will have this list as it's column headings. I have attempted this problem but with no success. My attempted query looks something like this: SELECT (SELECT DISTINCT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME LIKE '%201703%IMP') FROM (SELECT OBJECT_NAME(ST.object_id) AS POLICY_TYPE FROM sys.dm_db_partition_stats ST INNER JOIN sys.objects AS O ON O.object_id = ST.object_id WHERE (index_id < 2) AND O.type = 'U' AND OBJECT_NAME(ST.object_id) LIKE '%201703%IMP' GROUP BY ST.object_id) ORDER BY POLICY_TYPE ASC; The second part of this code addresses the existing tables to be considered under the column headings. I'm not sure if I'm even on the right track but any advice would be much appreciated, Thanks.