I have two tables:
Whenewher I'm removing some records from TableFirst I need to insert deleted record data into TableSecond. But they have different number of columns.
Is it possible to output deleted values of common columns using information schema(except Id of course) in delete statement?
DELETE FROM TableFirst
OUTPUT DELETED.* INTO TableSecond
WHERE Id = 1
I'm trying to avoid naming each column (I'll have up to 50 columns in my project).
Best Answer
I find out the way to do it with the dynamic sql, we just need to compose query, where we can select all required column names using information schema and execute it.
Get column names first:
Compose OUTPUT DELETED and column name strings:
Compose query:
Finaly we just execute the composed query:
But we can get issues when there are some FKs.