IF中的多个选择语句

时间:2009-11-12 17:45:06

标签: oracle select if-statement toad case-statement

我可以使用一些帮助为报告编写prog./sql构造..

Sql应首先检查提示&然后决定应该运行哪些“选择”语句

类似(伪代码)

Select ACCT,LOC FROM (

   IF :loc = 'MN' THEN
      Select acc as ACCT,location as LOC
   ELSE IF :loc = 'MA' THEN
      Select accid as ACCT,locid as LOC
   ELSE IF :loc = 'PA' THEN 
      Select accountid as ACCT,location as LOC
   END IF)

以上似乎不起作用。请帮忙!

3 个答案:

答案 0 :(得分:2)

你可以这样做:

Select acc as ACCT,location as LOC 
where :loc = 'MN' 
UNION ALL
Select accid as ACCT,locid as LOC 
where :loc = 'MA' 
UNION ALL
Select accountid as ACCT,location as LOC 
where :loc = 'PA';

答案 1 :(得分:2)

怎么样......

select acc,
       case :loc
       When 'MN' then location
       When 'MA' then locid
       When 'PA' then location
       ...
       end
from   ...

或......

select acc,
       case when :loc in ('MN','PA')
            then location
            When :loc in ('MA',...)
            then locid
            ...
       end
from   ...

这样做会更友好,因为它会减少唯一游标的数量。

答案 2 :(得分:1)

如果您可以创建自定义PL / SQL函数,那么您可以从中返回结果集(所谓的记录表),然后只从函数中选择。请参阅this article