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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/810919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 18:09:28  来源:igfitidea点击:

scalar subquery in if statement Condition in PL/SQL

oracleplsqlconditionalscalar-subquery

提问by Dinesh Manne

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....

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

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

I have similar Case statement which works fine

我有类似的 Case 语句,它工作正常

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

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.

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

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

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

回答by Dave Costa

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

不,您不能以您想要的方式使用 SELECT。

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.

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

回答by u07ch

Have you called DBMS_OUTPUT.ENABLE

你有没有叫 DBMS_OUTPUT.ENABLE

Quick example

快速示例

BEGIN
DBMS_OUTPUT.DISABLE;
DBMS_OUTPUT.PUT_LINE('Disabled');
DBMS_OUTPUT.ENABLE;
DBMS_OUTPUT.PUT_LINE('Enabled');
END;

回答by cagcowboy

I don't believe subqueries are supported in IF conditions... PL/SQL will be expecting the SELECT to give it a set of records, not a single value to be used in a expression/statement.

我不相信 IF 条件支持子查询...... PL/SQL 将期望 SELECT 给它一组记录,而不是在表达式/语句中使用的单个值。