简体   繁体   English

在SQL Server 2008中合并两个表

[英]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. 我有2个表一个是Stage表,其架构是exac到main,我想将数据从stage表更新到主表,ID列作为refrential键。 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. 我已经尝试在SQL中使用Merge但面临问题,因为只需要更新几个值,并且需要将数千个新值插入主表。 eg: 例如:

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:(
)

You can refer to the merge source in the insert part, like: 您可以在insert部分中引用合并源,例如:

when not matched then insert
  (id, name, email_id) 
  values (stage.id, stage.name, stage.email_id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM