if 语句中的标量子查询 PL/SQL 中的条件

2021-12-24 00:00:00 conditional oracle plsql scalar-subquery

我有一个类似于下面的 If 语句块,它因错误而失败 -PLS-00103:在期待以下之一时遇到符号SELECT"......

I have an If Statement block similar to the below which is failing with the error - PLS-00103: Encountered the symbol "SELECT" when expecting one of the following....

Begin
    If (select count(*) from Table1) > 0 then
        dbms_output.put_line('Test');
    end if;
end;

我有类似的 Case 语句,效果很好

I have similar Case statement which works fine

select 
case 
    when (select count(*) from Table1) > 0
        then 2
    else
        1
end
from dual

从我在 Oracle 文档中读到的 if 和 when 支持布尔表达式,任何想法是否在 If 条件中支持子查询.

From what i have read in Oracle Documentation the if and when support a Boolean Expression, any ideas whether Subqueries are supported in If Conditions.

注意:语句已经简化,我不是真的要得到整个表的计数,所以没有优化建议

Note: The Statements have been simplified, i am not really going to get the count of the entire table, so no optimization suggestions please

推荐答案

不,您不能以自己想要的方式使用 SELECT.

No, you can't use a SELECT in the way you want.

在您使用 CASE 的示例中,您没有使用 CASE语句"——您使用的是 CASE 表达式,它恰好嵌入在 SQL 语句中.在这种情况下,您可以使用子查询,因为它位于 SQL 语句的上下文中,而不是过程语句.您将无法在过程 CASE 语句中使用这样的子查询.

In your example using CASE, you are not using a CASE "statement" -- you are using a CASE expression, which happens to be embedded within a SQL statement. You can use a subquery in that case because it's within the context of a SQL statement, not a procedural statement. You wouldn't be able to use a subquery like this in a procedural CASE statement.

相关文章