首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当目标表为空时,Oracle SQL merge不会插入

当目标表为空时,Oracle SQL merge不会插入
EN

Stack Overflow用户
提问于 2020-07-06 14:56:35
回答 1查看 109关注 0票数 0

我想使用merge语句在工作模式中更新/插入主模式中的记录。执行合并的代码片段如下所示。

代码语言:javascript
复制
MERGE INTO ods_workday.ORGANIZATION x
                USING (SELECT organization_id, organization_code, organization_type, organization_name, organization_description,  
                inactive, manager, leadership_role_assignee, organization_owner, supervisory_organization, 
                primary_site_reference, inactive_date, organization_subtype
                FROM ods_workday_w.ORGANIZATION) y
                ON (x.ORGANIZATION_ID = y.ORGANIZATION_ID)
                WHEN MATCHED THEN
                    UPDATE SET x.organization_code = y.organization_code, 
                                        x.organization_type = y.organization_type,
                                        x.organization_name = y.organization_name,
                                        x.organization_description = y.organization_description, 
                                        x.inactive = y.inactive,
                                        x.manager = y.manager, 
                                        x.leadership_role_assignee = y.leadership_role_assignee,
                                        x.organization_owner = y.organization_owner, 
                                        x.supervisory_organization = y.supervisory_organization,
                                        x.primary_site_reference = y.primary_site_reference,
                                        x.inactive_date = y.inactive_date, 
                                        x.organization_subtype = y.organization_subtype,
                                        x.UPDATE_TIME_STAMP = CURRENT_TIMESTAMP
                    WHERE x.organization_code <> y.organization_code OR
                                        x.organization_type <> y.organization_type OR
                                        x.organization_name <> y.organization_name  OR
                                        x.organization_description <> y.organization_description OR
                                        x.inactive <> y.inactive OR
                                        x.manager <> y.manager OR
                                        x.leadership_role_assignee <> y.leadership_role_assignee OR
                                        x.organization_owner <> y.organization_owner OR
                                        x.supervisory_organization <> y.supervisory_organization OR
                                        x.primary_site_reference <> y.primary_site_reference OR
                                        x.inactive_date <> y.inactive_date OR
                                        x.organization_subtype <> y.organization_subtype       
                WHEN NOT MATCHED THEN
                    INSERT(x.organization_id,  x.organization_code, x.organization_type, x.organization_name,
                    x.organization_description, x.inactive, x.manager, x.leadership_role_assignee,
                    x.organization_owner, x.supervisory_organization, x.primary_site_reference, x.inactive_date,
                    x.organization_subtype, x.UPDATE_TIME_STAMP)  
                    VALUES(y.organization_id,  y.organization_code, y.organization_type, y.organization_name,
                    y.organization_description, y.inactive, y.manager, y.leadership_role_assignee,
                    y.organization_owner, y.supervisory_organization, y.primary_site_reference, y.inactive_date,
                    y.organization_subtype, CURRENT_TIMESTAMP);

当目标表有记录,但目标表为空(没有任何记录比合并)时,代码工作得很好。

当目标模式中没有要开始的记录时,有没有一种方法可以让merge语句工作。

Oracle数据库是11g。

EN

回答 1

Stack Overflow用户

发布于 2020-07-06 15:11:59

这已经解决了。我没有添加源表中存在的外键约束,因为在零记录上的插入不起作用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62750766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档