Unix Sql - 从第二个表更新一个表 - 空值问题

时间:2016-02-18 19:56:53

标签: sql oracle

我一直在寻找网络数小时试图确定我的代码有什么问题。我一直收到ORA-01407:无法更新(" AMIOWN"。"会员"。" LANGUAGE_X")为NULL。当合同表中的语言不为null时,我需要更新成员表中的语言。需要第3个表才能深入查看特定成员。以下是代码:

update member m
    set language_x =
    (select language_x
        from contract c
        where m.contract_nbr  = c.contract_nbr and
        c.language_x is not null and
        m.member_nbr =
        (select member_nbr
            from member_span ms
            where m.member_nbr = ms.member_nbr and
            ms.void     = ' ' and
            ms.carrier  = 'PM' and
            ms.prog_nbr = 'GP' and
            ms.region   = 'TR' and
            (ms.ymdeff <= 20160218 or ms.ymdeff > 20160218) and
            ms.ymdend > 20160218
        )
    );

有些帖子还建议在最后一个括号检查后添加另一行:

where exists(从合同c中选择1,其中m.contract_nbr = c.contract_nbr和c.language_x不为空);

我在unix环境中工作,表包含在amisys数据库中。

感谢您提出任何建议。

1 个答案:

答案 0 :(得分:0)

您需要将where条件添加到更新语句

update member m
    set language_x = (select language_x
                        from contract c
                       where m.contract_nbr  = c.contract_nbr 
                         and c.language_x is not null 
                         and m.member_nbr = (select member_nbr
                                              from member_span ms
                                             where m.member_nbr = ms.member_nbr 
                                               and ms.void     = ' ' 
                                               and ms.carrier  = 'PM' 
                                               and ms.prog_nbr = 'GP' 
                                               and ms.region   = 'TR' 
                                               and (ms.ymdeff <= 20160218 or ms.ymdeff > 20160218) 
                                               and ms.ymdend > 20160218)
    )
    where exists (select language_x
                        from contract c
                       where m.contract_nbr  = c.contract_nbr 
                         and c.language_x is not null 
                         and m.member_nbr = (select member_nbr
                                              from member_span ms
                                             where m.member_nbr = ms.member_nbr 
                                               and ms.void     = ' ' 
                                               and ms.carrier  = 'PM' 
                                               and ms.prog_nbr = 'GP' 
                                               and ms.region   = 'TR' 
                                               and (ms.ymdeff <= 20160218 or ms.ymdeff > 20160218) 
                                               and ms.ymdend > 20160218)
    );

下面是一个更优雅的解决方案,但根据您的数据模型,它可能会或可能不会工作

Update    
(
select m.language_x as oldval 
       c.language_x as newval
  from member m
      ,contract c
where m.contract_nbr  = c.contract_nbr 
  and c.language_x is not null 
  and m.member_nbr = (select member_nbr
                        from member_span ms
                       where m.member_nbr = ms.member_nbr 
                         and ms.void     = ' ' 
                         and ms.carrier  = 'PM' 
                         and ms.prog_nbr = 'GP' 
                         and ms.region   = 'TR' 
                         and (ms.ymdeff <= 20160218 or ms.ymdeff > 20160218) 
                         and ms.ymdend > 20160218)
) t
set t.oldval =t.newval