Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two Table in SQL Server 2008

I have 2 table one is a Stage table whose schema is exac to main,i want to update the data from stage table to main table with the ID column as refrential key. I have tried using Merge in SQL but facing problems with that as there are only few values to be updated and thousand of new values need to be inserted to the main table. e.g:

MERGE TABLE tblMain AS main
USING (SELECT ID,NAME,EMAIL_ID FROM tblStage) as stage
ON main.ID=stage.ID
WHEN MATCHED THEN UPDATE SET
main.ID=stage.ID,
main.NAME=stage.NAME,
main.EMAIL_ID=stage.EMAIL_ID
WHEN NOT MATCHED THEN INSERT VALUES 
(
----I am stucked here what to write as there are thousands of values:(
)
like image 241
Pratik Avatar asked Nov 05 '22 00:11

Pratik


1 Answers

You can refer to the merge source in the insert part, like:

when not matched then insert
  (id, name, email_id) 
  values (stage.id, stage.name, stage.email_id)
like image 109
Andomar Avatar answered Nov 07 '22 20:11

Andomar