【问题标题】:oracle 10g pl/sql conditional for loop selectoracle 10g pl/sql 有条件的循环选择
【发布时间】:2012-11-14 22:25:05
【问题描述】:

在 10g 的 PL/SQL 中可以做这样的事情吗?

if user_is_goat = 1 then
  for item_rec in (select * from pricing_for_goats)
else 
  for item_rec in (select * from pricing_for_non_goats)
end if;
loop
.
.
end loop;

似乎当 oracle 看到“for rec in select * from dual”时,它期望“循环”立即跟随。我在循环中的代码很多行,我不想维护它的 2 个副本。

【问题讨论】:

    标签: plsql oracle10g


    【解决方案1】:

    试试下面的查询,这将检查变量user_is_goat = 1是否从for_goats返回数据,否则它将从for_non_goats返回

    for item_rec in 
    (
      select * from pricing_for_goats where user_is_goat = 1
      union
      select * from pricing_for_non_goats where user_is_goat <> 1
    )
    loop
    .....
    .....
    end loop;
    

    【讨论】:

    • 我喜欢它,但只有两个表兼容才能工作
    • 所以你不能用你需要的列列表替换*。你必须知道它们是什么,因为你在循环中使用它们,对吧?
    猜你喜欢
    • 2012-02-26
    • 2010-11-03
    • 2014-08-05
    • 2019-04-06
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    • 1970-01-01
    • 2010-09-15
    相关资源
    最近更新 更多