Oracle PL / SQL:创建短路查询

问题描述 投票:0回答:1

我有点老,所以我称之为短路。

但是我在Oracle中有3个表。我也有一个“ ProcessFlag”。如果表中的任何数据均符合我的条件,我想将procesdFlag设置为1。

我显然可以使用if和else语句来做到这一点。

 select count(*) into myCounter from from Table1 where processflag = 1;
 if myCounter = 0 then
      select count(*) into myCounter from Table2 where processflag = 1;
      if myCounter = 0 then
           select count(*) into myCounter from Table3 where processflag = 1;
           if myCounter = 0 then
                 processFlag = 0;
           else
                 processFlag = 1;
           end if
      else
           processFlag = 1;
      end if
 else
       processFlag = 1;
 end if

所以,假设我对我的if / eles都是正确的,我想你明白我在想做什么。如果任何表中的任何行具有processFlag = 1,那么我要处理。

这只是一个演示的小例子,但是我的现实世界中大约有10个表,如果不需要,我真的不想嵌套if / else语句那么深。而且其中一些表包含大量数据,因此,如果在第一个表中找到了某些内容,则无需检查后续表(这就是为什么我将其称为“短路”的原因,因为一旦发现某些内容,就不会需要处理剩余的代码)。

如果在Oracle中有更干净/有效的方法来做到这一点?

sql oracle oracle11g oracle10g
1个回答
0
投票

您想要这样的东西吗?

select . . .
from . . .
where exists (select 1 from table1 where processflag = 1) or
      exists (select 1 from table2 where processflag = 1) or
      exists (select 1 from table3 where processflag = 1) ;

也就是说,您可以使查询以一堆exists条件为条件。如果使用的是PL / SQL,则可以使用类似于实际设置标志的方法。

© www.soinside.com 2019 - 2024. All rights reserved.