I have a temp table with few table names as below:
Id TableName
-------------
1 TableA
2 TableB
3 TableC
These table names should be replaced in the query shown, and should get executed. I can achieve it with a while
loop, but I don't want to use while. Is there any alternative concept?
I have the following SQL statement:
SELECT
TDU.ColHeader, TDU.ColValues
FROM
(SELECT
' + @ColumnsCasted + '
FROM
' + @TableName + ' ' +
@WhereCondition + ') TD
UNPIVOT
(ColValues FOR ColHeader IN (' + @ColumnsUnpivot + ')
) AS TDU;
The @TableName
, @ColumnsCasted
, @ColumnsUnpivot
are based upon the table name which is stored in a temp table. I used while loop to iterate each table name and replace it here in the statement.
Can anyone suggest a method without using while loop?