0

When trying to insert a value into a table coming from a dynamic SQL this is working perfectly.

INSERT TableName(ColumnName1) EXEC('SELECT '+@ColumnName+' from kat.[dbo].[History] ')

However, when I want to insert more than 1 value with this Dynamic SQL this is not working anymore.

INSERT TableName(ColumnName1, ColumnName2) EXEC('SELECT '+@ColumnName+' from kat.[dbo].[History] '), datum from kat.[dbo].[History] .

Is there a reason for this?

May thanks in advance,

Kat

1 Answer 1

1

Your first statement, the dynamic SQL is returning a single, distinct result set, which is being inserted into your table. Your second statement, your treating it like it's a column, which it's not. Since you're pulling from the same table, you could either include datum inside your dynamic SQL, or join back out to dbo.history after the fact

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Xedni, Thanks for the answer. You mean to create the following : INSERT TableName(ColumnName1, ColumnName2) EXEC('SELECT '+@ColumnName+', datum from kat.[dbo].[History] ')
That was my thought, yes.
Hi, thanks for this. You are right, this is working, as both columns are in the same table. The problem comes when you want to combine different dynamic queries from different tables or constants, concats, etc...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.