Skip to content
Advertisement

Copy all values from source table to destination table dynamically in SQL Server. If there is some value in destination table then delete and insert

I have created 2 tables and populated the values in my source table.

Now, I want to populate these values into the destination table dynamically and also check if there is any value in the destination table then delete all those values and insert them from the source.

This is my approach which is not executing:

Advertisement

Answer

In your select where you are pulling the destination and source table names, they are the same table. So, you’re deleting from the table and then trying to insert back into the same table.

I’m assuming your Customer_new table is the destination table and Customer is the source table.

You should just need to update the following line to append the _new to the end of the destination table name by adding + '_new' to the destinationation table part of the select statement:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement