简体   繁体   English

如何在合并语句中使用来自其他表的 select 插入?

[英]How can I use insert with select from other table in a merge statement?

I have this query.我有这个查询。

MERGE sales.category t 
    USING sales.category_staging s
ON (s.category_id = t.category_id)
WHEN MATCHED
    THEN UPDATE SET 
        t.category_name = s.category_name,
        t.amount = s.amount
WHEN NOT MATCHED BY TARGET 
    THEN INSERT t(category_id, category_name, amount)
         VALUES select s.category_id, s.category_name, s.amount from s
WHEN NOT MATCHED BY SOURCE 
    THEN DELETE;

How can I use insert with select from other table in a merge statement?如何在合并语句中使用来自其他表的 select 插入?

You wouldn't.你不会的。 You have already defined S so you can reference those columns in the VALUES clause.您已经定义了S ,因此您可以在VALUES子句中引用这些列。 You also don't put the alias of the destination table prior to the destination columns:您也不要将目标表的别名放在目标列之前:

    THEN INSERT (category_id, category_name, amount)
         VALUES (s.category_id, s.category_name, s.amount)

This might be helpful for your requirement to use the other table. 可能对您使用其他表的要求有所帮助。

You can use the join as a Source and then you can use it for Insert as well.您可以将连接用作源,然后也可以将其用于插入。

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

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