暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 更新面向场景的ORA-01427

ASKTOM 2020-02-12
733

问题描述

你好

请参见下面的更新声明...
运行这个时,我面临着ORA-01427
还尝试过 “... 存在的地方 (选择1...”,但没有正确

我需要更新TBLTARGET中的TAXINV和TAXINVDATE列,其中TAXINV为null,
从TBLSOURCE中的SALEBILLNO和SALEBILLDATE列中提取相应的值
这两个表由EXCISEINVNO值连接在一起,出现在两个表中

希望我能解释...

我认为这更多的是一个概念问题
但是必须做好这件事,因为我需要处理更多这样的情况

请帮助
谢谢

注:
1.数据库是11g R2
2.使用SQL开发人员
3.测试用例表仅包含表示所需的最小行


update tblTarget s1
set (s1.taxinvno, s1.taxinvdate)
=
(
select sb2.salebillno, sb2.salebilldate
from tblTarget se1
left join 
(select * from tblSource
where yearcode='2017-2018' and exciseinvdate>='01-JUL-17') sb2
on se1.exciseinvno=sb2.exciseinvno
where se1.yearcode='2017-2018' and se1.exciseinvdate>='01-JUL-17'
and taxinvno is null
) 
--where exists (???)
;

--Error report -
--ORA-01427: single-row subquery returns more than one row



Addendum / 12-Feb-2020
The result of the SELECT below will give you a visual of what I want to update -


select se1.channelcode, se1.exciseinvno, se1.exciseinvdate, taxinvno, taxinvdate, 
sb2.exciseinvno as exciseinvno_sb2 , sb2.exciseinvdate as exciseinvdate_sb2, sb2.salebillno, sb2.salebilldate
from tblTarget se1
left join 
(select * from tblSource
where yearcode='2017-2018' and exciseinvdate>='01-JUL-17') sb2
on se1.exciseinvno=sb2.exciseinvno
where se1.yearcode='2017-2018' and se1.exciseinvdate>='01-JUL-17'
and taxinvno is null


The Columns TAXINVNO, TAXINVDATE, with null values in TBLTARGET,
have to be updated with SALEBILLNO and SALEBILLDATE from TBLSOURCE respectively

专家解答

几件事:

-要过滤要更新的行,请将where子句从se1的SET子查询移动到更新本身

-无需在SET子查询中加入tblTarget; 您可以从更新中访问所需的值

-我不确定您是否需要EXISTS条款!

在示例数据中,移动where子句标识要更新的行。您正在搜索taxinvno为null的行。

如果tblSource中没有匹配的行,则更新后taxinvno仍为null。所以你不需要担心覆盖现有值。

所以我相信你可以使用:

update tblTarget s1
set   (s1.taxinvno, s1.taxinvdate) = (
  select sb2.salebillno, sb2.salebilldate
  from   tblSource  sb2
  where  s1.exciseinvno=sb2.exciseinvno
  and    sb2.yearcode='2017-2018' 
  and    sb2.exciseinvdate>='01-JUL-17'
) 
where s1.yearcode='2017-2018' 
and   s1.exciseinvdate>='01-JUL-17'
and   taxinvno is null


但如果你真的想要存在,使用这个;

update tblTarget s1
set   (s1.taxinvno, s1.taxinvdate) = (
  select sb2.salebillno, sb2.salebilldate
  from   tblSource  sb2
  where  s1.exciseinvno=sb2.exciseinvno
  and    sb2.yearcode='2017-2018' 
  and    sb2.exciseinvdate>='01-JUL-17'
) 
where s1.yearcode='2017-2018' 
and   s1.exciseinvdate>='01-JUL-17'
and   taxinvno is null
and   exists (
  select * from tblsource sb2
  where s1.exciseinvno = sb2.exciseinvno
  and  sb2.yearcode = '2017-2018'
  and  sb2.exciseinvdate >= '01-JUL-17'
);

文章转载自ASKTOM,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论