【问题标题】:Oracle - Subquery in IF Clause [duplicate]Oracle - IF 子句中的子查询 [重复]
【发布时间】:2021-05-30 08:55:18
【问题描述】:

我有以下代码:

if USER IN (SELECT user_id from user_maint where roll = 12) THEN
        p_status := 'W';

ELSE 
        p_status := 'A';
END IF;

它给出了错误:Error(332,16): PLS-00405: subquery not allowed in this context

如何在IF子句中进行子查询?

【问题讨论】:

  • 什么是用户?是变量吗?
  • 这是一个从当前 Oracle 会话 @Kazi 返回 user_id 的函数。
  • 没问题,@Kazi :)
  • 如何在 IF 子句中进行子查询? 假设这是 PL/SQL 可以做到的。您真正想做的是使用查询来测试条件。

标签: sql oracle if-statement


【解决方案1】:

首先使用Select,然后使用if

declare
  l_user_id user_maint.user_id%type;
  p_status  varchar2(1);
begin
  select user_id
  into l_user_id
  from user_maint
  where roll = 12;

  if l_user_id = user then
     p_status := 'W';
  else
     p_status := 'A';
  end if;
end;
/

不过,更简单的选择是

select case when user_id = user then 'W'
            else 'A'
       end
into p_status
from user_maint
where roll = 12;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 2011-07-21
    相关资源
    最近更新 更多