ORACLE-CASE 需要 INTO?
当我尝试以下操作时:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已将 SQL 包装在开始/结束块中,这使其成为 PL-SQL 语句。在 PL-SQL 中,Oracle 期望有一个地方来放置 SQL 的结果集。这就是 ORA-06550 错误的根源。您需要声明一个 PL-SQL 变量来保存通过 INTO 查询的结果。
但 SELECT 语句还是有点奇怪。按照您的方式,在 FROM 子句中拥有真正的表“tableOfBeans”是没有意义的。
你想要这样的东西吗?
实际上,如果 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?
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.
CASE
语句不需要INTO
。ORA-6550 是 SQL 无法编译时的一般错误。该错误报告它认为问题是没有
INTO
子句,因为它认为您正在尝试执行 SELECT INTO 语句,如下所示:我没有要测试的 Oracle 安装,但我发现 Oracle 9i/10g 对 CASE 语句很奇怪,有时要求您使用
END CASE
而不仅仅是END
。END CASE
是 Oracle 文档中提到了此处。通常您会使用 DUAL 来处理此类事情,测试变量是否已设置:
The
CASE
statement doesn't require anINTO
.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: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 justEND
.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:
正如之前所回答的,错误表明您必须将选择的结果放入变量中。
如果您的表 tableOfBeans 包含多于一行,您将遇到另一个错误 ORA-01422:精确获取返回多行。使用伪表对偶代替或遵循 pl/sql 代码。
As answered before the errors says that you have to put the result of the select into a variable.
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.