5

我有 2 个表,其中一个是 Stage 表,其模式与 main 完全一致,我想将数据从 stage 表更新到 main 表,并将 ID 列作为 refrential 键。我曾尝试在 SQL 中使用 Merge,但遇到了问题,因为只有很少的值需要更新,并且需要将数千个新值插入到主表中。例如:

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

1 回答 1

2

您可以参考insert部分中的合并源,例如:

when not matched then insert
  (id, name, email_id) 
  values (stage.id, stage.name, stage.email_id)
于 2012-05-08T09:20:45.420 回答