SQL Server Is there a way to select columns from a given list

Viewed 2845

I have a list of columns that are dynamic then select the column in table that are in the list.

For example

Select (List of columns) 
from tablename

Here is my code

SET NOCOUNT ON;

DECLARE @table TABLE(cols varchar(50));

DECLARE @tablename NVARCHAR(255);
DECLARE @tableid INT;
DECLARE @sSQL NVARCHAR(500);

SET @tableid = (SELECT DISTINCT TABLEID FROM faciltyShowedDetails WHERE layerid = @layerid);
SET @tablename = (SELECT dbo.FNC_Search_GetSearchTable(@tableid))

INSERT INTO @table(cols)
    SELECT COLUMN_NAME 
    FROM faciltyShowedDetails 
    WHERE layerid = @layerid;

I want to select from the list of column in @table.

1 Answers
Related