ORACLE-CASE 需要 INTO?

发布于 2024-08-13 10:06:21 字数 491 浏览 2 评论 0原文

当我尝试以下操作时:

declare var_type VARCHAR2(10);

begin

  var_type := 'B';

  select case var_type 
           when 'B' then 'Beans' 
           when 'L' then 'Legumes' 
         end 
    from tableOfBeans ;

end;

我收到一条错误消息

ORA-06550: 第 4 行,第 1 列:
PLS-00428:此 SELECT 语句中需要 INTO 子句
在位置 #:62 检测到错误

但是当我不使用 var_type 而是使用“B”作为条件时,效果很好。谁能告诉我为什么会发生这种情况以及如何修复它以便我可以正确使用 var_type

When I try the following:

declare var_type VARCHAR2(10);

begin

  var_type := 'B';

  select case var_type 
           when 'B' then 'Beans' 
           when 'L' then 'Legumes' 
         end 
    from tableOfBeans ;

end;

I get an error saying

ORA-06550: line 4, column 1:
PLS-00428: an INTO clause is expected in this SELECT statement
Error detected at position #:62

But when I don't use var_type but instead use 'B' as the condition it works nicely. Can anyone tell me why this happens and how do I fix it so I can use var_type properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谈情不如逗狗 2024-08-20 10:06:21

您已将 SQL 包装在开始/结束块中,这使其成为 PL-SQL 语句。在 PL-SQL 中,Oracle 期望有一个地方来放置 SQL 的结果集。这就是 ORA-06550 错误的根源。您需要声明一个 PL-SQL 变量来保存通过 INTO 查询的结果。

但 SELECT 语句还是有点奇怪。按照您的方式,在 FROM 子句中拥有真正的表“tableOfBeans”是没有意义的。

你想要这样的东西吗?

declare var_type VARCHAR2(10);

begin

    select case tableOfBeans.beanType
               when 'B' then 'Beans'
               when 'L' then 'Legumes'
           end
      into var_type
      from tableOfBeans;

end;

实际上,如果 tableOfBeans 中有不止一行,这也会产生错误。也许您想要的是从 tableOfBeans 中检索项目,其中 tableOfBeans.beanType = var_type。在这种情况下,您仍然需要一种存储结果集的方法。抱歉,如果我不明白你想做什么。更详细一点会有帮助。

You have wrapped your SQL in a begin/end block, which makes it a PL-SQL statement. In PL-SQL, Oracle is expecting a place to put the result set from the SQL. That's the source of your ORA-06550 error. You'll want a PL-SQL variable declared to hold the result of the query via the INTO.

But the SELECT statement is still a little weird. The way you have it, there's no point in having a real table "tableOfBeans" in the FROM clause.

Did you want something like this?

declare var_type VARCHAR2(10);

begin

    select case tableOfBeans.beanType
               when 'B' then 'Beans'
               when 'L' then 'Legumes'
           end
      into var_type
      from tableOfBeans;

end;

Actually, that would generate an error too if you had more than one row in tableOfBeans. Maybe what you wanted was to retrieve items from tableOfBeans where tableOfBeans.beanType = var_type. In that case you'd still need a way to store the result set. Sorry if I'm not understanding what you're trying to do. A little more detail will help.

最笨的告白 2024-08-20 10:06:21

CASE 语句不需要 INTO
ORA-6550 是 SQL 无法编译时的一般错误。该错误报告它认为问题是没有 INTO 子句,因为它认为您正在尝试执行 SELECT INTO 语句,如下所示:

SELECT t.column
  INTO var_type
  FROM tableOfBeans

我没有要测试的 Oracle 安装,但我发现 Oracle 9i/10g 对 CASE 语句很奇怪,有时要求您使用 END CASE 而不仅仅是 ENDEND CASEOracle 文档中提到了此处

通常您会使用 DUAL 来处理此类事情,测试变量是否已设置:

var_type := 'B';

SELECT CASE var_type 
         WHEN 'B' then 'Beans' 
         WHEN 'L' then 'Legumes' 
       END CASE
  FROM DUAL;

The CASE statement doesn't require an INTO.
ORA-6550 is a generic error for when your SQL doesn't compile. The error is reporting that it thinks the issue is there's no INTO clause because it thinks you're trying to perform a SELECT INTO statement, like this:

SELECT t.column
  INTO var_type
  FROM tableOfBeans

I don't have an Oracle install to test against, but I have experienced Oracle 9i/10g being weird with CASE statements, sometimes requiring you to use END CASE rather than just END. END CASE is mentioned in the Oracle documentation here.

Usually you'd use DUAL for this sort of thing, testing if the variable is set:

var_type := 'B';

SELECT CASE var_type 
         WHEN 'B' then 'Beans' 
         WHEN 'L' then 'Legumes' 
       END CASE
  FROM DUAL;
橘和柠 2024-08-20 10:06:21

正如之前所回答的,错误表明您必须将选择的结果放入变量中。

declare 
    var_type VARCHAR2(10);
    var_into VARCHAR2(10);
begin
    var_type := 'B';

    select case var_type 
           when 'B' then 'Beans'
           when 'L' then 'Legumes'
    end
    into var_into
    from tableOfBeans;
end;

如果您的表 tableOfBeans 包含多于一行,您将遇到另一个错误 ORA-01422:精确获取返回多行。使用伪表对偶代替或遵循 pl/sql 代码。

declare 
    var_type VARCHAR2(10);
    var_into VARCHAR2(10);
begin
    var_type := 'B';

    var_into := case var_type 
           when 'B' then 'Beans'
           when 'L' then 'Legumes'
    end;
end;

As answered before the errors says that you have to put the result of the select into a variable.

declare 
    var_type VARCHAR2(10);
    var_into VARCHAR2(10);
begin
    var_type := 'B';

    select case var_type 
           when 'B' then 'Beans'
           when 'L' then 'Legumes'
    end
    into var_into
    from tableOfBeans;
end;

If your table tableOfBeans contains more than one row you will run into another error ORA-01422: exact fetch returns more than one row. Use pseudo table dual instead or following pl/sql code.

declare 
    var_type VARCHAR2(10);
    var_into VARCHAR2(10);
begin
    var_type := 'B';

    var_into := case var_type 
           when 'B' then 'Beans'
           when 'L' then 'Legumes'
    end;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文