SQL中if语句中的标量子查询

SQL中if语句中的标量子查询

本文介绍了PL/SQL中if语句中的标量子查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于以下内容的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条件中是否支持子查询.

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.

这篇关于PL/SQL中if语句中的标量子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 22:01