小编典典

oracle查询中的错误单行子查询返回多个行

sql

任何想法在这里是什么问题?如何将下面的Sybase查询转换为Oracle。

Sybase查询

Update student  s1 set s1.delay = (select  date1 - date2 from cource c where c.ID = c1.ID and 
c.value  ='On' and c.Act_id = 
select max(Act_id) from cource c2 where c2.Id = C.id and c2.value ='On')
from student s1, Cource c1
where c1.id = s1.id and 
c1.value ='On' and 
s1.status = 'active' and 
s1.currentprofile = s1.prevProfile

将上面的查询转换为oracle后,出现此错误“单行子查询返回多个行”

Update student  s1 set s1.delay = (select (select  date1 - date2 from cource c where c.ID = c1.ID and             
c.value  ='On' and c.Act_id = 
select max(Act_id) from cource c2 where c2.Id = C.id and c2.value ='On')
from student s1, Cource c1
where c1.id = s1.id and 
c1.value ='On' and 
s1.status = 'active' and 
s1.currentprofile = s1.prevProfile)
Where Exists
(select (select  date1 - date2 from cource c where c.ID = c1.ID and c.value  ='On' and c.Act_id = 
select max(Act_id) from cource c2 where c2.Id = C.id and c2.value ='On')
from student s1, Cource c1
where c1.id = s1.id and 
c1.value ='On' and 
s1.status = 'active' and 
s1.currentprofile = s1.prevProfile)

阅读 168

收藏
2021-04-15

共1个答案

小编典典

Sybase查询(出于解释目的):

update student s1 
   set s1.delay = 
   (select (date1 -date2) 
      from cource c 
     where ****c.id = c1.id****(II)
       and c.value  ='On' 
       and c.Act_id = select max(Act_id) 
                        from cource c2 
                       where c2.Id = C.id 
                         and c2.value ='On')
  ****from student s1
     , cource c1****(I)
 where c1.id = s1.id 
   and c1.value ='On' 
   and s1.status = 'active' 
   and s1.currentprofile = s1.prevProfile;

更新时,我们可以看到两个主要条件,

  1. 首先,如果您看到这一部分,请****from student s1 , cource c1****(I)确保仅更新student表中具有匹配项idcource表中的行以及更多条件,并且由于Oracle不允许直接在语句的from子句中进行此类检查update,因此可以用exists下面的Oracle查询中可以看到的子句代替。

  2. 其次,部分****c.id = c1.id****(II)上面的Sybase查询确保它进一步只是获取数据的setBY子句co-relateids我们的第一步,并为甲骨文这个发现,我们需要用它也被更新,即实际的表来代替student,因为我们已经确保与exists在第一个步骤是什么ids,必须更新。

Oracle查询(实际查询):

update student s1 
   set s1.delay = (select (date1 - date2)
                     from cource c 
                    where c.id = s1.id 
                      and c.value  ='On' 
                      and c.act_id = select max(act_id) 
                                       from cource c2 
                                      where c2.Id = c.id 
                                        and c2.value ='On')
from student s1
where s1.status = 'active' 
  and s1.currentprofile = s1.prevprofile
  and exists (select 1 
                from cource c1
               where c1.id = s1.id 
                 and c1.value ='On');
2021-04-15