目前我有一个使用Cursor的大型存储过程,里面是两个合并语句。不久之后,我想从一张大桌子上填充2张桌子。
现在,我对sql很新,并且已经阅读了相当多的内容以扩展我的知识。在这项研究中,我遇到了许多主题,他们讨论了游标和他们可能给出的性能问题。
当前的SP(带光标)工作正常,但是我试图在没有光标的情况下使用我的代码(其他POV)而且它会让我感到害怕。
所以现在我坚持使用这一小块垃圾代码。我认为不可能按照我想要的方式去做,但是嘿,我可以尝试一下。
错误:子查询返回的值超过1。当子查询遵循=,!=,<,...
时,不允许这样做代码:
MERGE CONTACTS as target
USING STAGING_CONTACTS as source
ON (target.ID = source.ID)
WHEN MATCHED THEN
UPDATE SET
MAIL = (SELECT MAIL FROM STAGING_CONTACTS),
NAME = (SELECT NAME FROM STAGING_CONTACTS),
BRAND = (SELECT BRAND FROM STAGING_CONTACTS),
ID = (SELECT ID FROM STAGING_CONTACTS)
WHEN NOT MATCHED THEN
INSERT (MAIL, NAME, BRAND, CORESOLEIL_ID)
VALUES ((SELECT MAIL FROM STAGING_CONTACTS),
(SELECT NAME FROM STAGING_CONTACTS),
(SELECT BRAND FROM STAGING_CONTACTS),
(SELECT ID FROM STAGING_CONTACTS));
这是我拥有的两个合并语句之一,这是较小的一个。
欢迎任何反馈!
提前致谢:)
答案 0 :(得分:2)
删除select语句.Below语句负责使用哪个表来更新哪个表..
MERGE CONTACTS as target
USING STAGING_CONTACTS as source
所以您的查询可以如下所示
UPDATE SET
MAIL = MAIL ,
and so on...
WHEN NOT MATCHED THEN
INSERT (MAIL, NAME, BRAND, CORESOLEIL_ID)
VALUES (mail,name,brand,id)
您也可以使用下面的别名
MERGE CONTACTS as target
USING STAGING_CONTACTS as source
ON (target.ID = source.ID)
WHEN MATCHED THEN
UPDATE SET
target.MAIL = source.MAIL,
and so on..
WHEN NOT MATCHED THEN
INSERT (MAIL, NAME, BRAND, CORESOLEIL_ID)
VALUES (source.all columns....)
答案 1 :(得分:0)
使用以下脚本..
MERGE CONTACTS as target
USING STAGING_CONTACTS as source
ON (target.ID = source.ID)
WHEN MATCHED THEN
UPDATE SET
target.MAIL = source.MAIL ,
target.NAME = source.Name,
target.BRAND = source.BRAND ,
target.ID = source.ID
WHEN NOT MATCHED THEN
INSERT (MAIL, NAME, BRAND, CORESOLEIL_ID)
VALUES ( source.MAIL , source.Name, source.BRAND ,source.ID );