I'm working on a query where I need to insert rows into a table and a temp table that have the same column names.
I would like to avoid repeating the column names in each insert statement and would prefer to create a list and just pass the list to the insert statements.
Here's what I have so far:
DECLARE @columnNameList varchar(MAX) = COALESCE(RecType, ColumnName,
SourcePosition, MaxWidth, isNullable, dotNetDataType, [Format])
EXEC('INSERT INTO #RowList (' + @columnNameList + ')
VALUES......
This almost works. I get an error for each column name that looks like this:
Invalid column name 'RecType'
I think the error is because the column name has single quotes around it as it is being converted to a string.
Is there a way to build a list of column objects rather than strings to pass in?