I am using staging tables to perform validation and insert into live.
Suppose I have a table PERSONS
TABLE Persons
(
Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
HouseNumber int,
)
and a STAGING TABLE as follows
TABLE Persons_Staging
(
Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
HouseNumber varchar(255),
)
I need to write a procedure to transfer data from the staging table to the live table while ensuring no duplicates are inserted. How can I achieve that?
Thanks in advance
Use the
MERGEcommand.Something like this:
If you want to update existing records you uncomment the
UPDATEpart and add a suitable update clause. The same with the delete part.