[英]ORACLE- CASE requires an INTO?
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:
ORA-06550:第4行第1列:
PLS-00428: an INTO clause is expected in this SELECT statementPLS-00428:此SELECT语句中需要一个INTO子句
Error detected at position #:62在#位置检测到错误:62
But when I don't use var_type
but instead use 'B' as the condition it works nicely. 但是当我不使用
var_type
但是使用'B'作为条件时,它可以很好地工作。 Can anyone tell me why this happens and how do I fix it so I can use var_type
properly? 任何人都可以告诉我为什么会发生这种情况,我该如何修复它以便我可以正确使用
var_type
?
You have wrapped your SQL in a begin/end block, which makes it a PL-SQL statement. 您已将SQL包装在开始/结束块中,这使其成为PL-SQL语句。 In PL-SQL, Oracle is expecting a place to put the result set from the SQL.
在PL-SQL中,Oracle期望从SQL中放置结果集。 That's the source of your ORA-06550 error.
这是您的ORA-06550错误的来源。 You'll want a PL-SQL variable declared to hold the result of the query via the INTO.
您需要声明一个PL-SQL变量来通过INTO保存查询结果。
But the SELECT statement is still a little weird. 但SELECT语句仍然有点奇怪。 The way you have it, there's no point in having a real table "tableOfBeans" in the FROM clause.
你拥有它的方式,在FROM子句中有一个真正的表“tableOfBeans”是没有意义的。
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. 实际上,如果tableOfBeans中有多行,那么也会产生错误。 Maybe what you wanted was to retrieve items from tableOfBeans where tableOfBeans.beanType = var_type.
也许你想要的是从tableOfBeans中检索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.
更多细节将有所帮助。
The CASE
statement doesn't require an INTO
. CASE
声明不需要INTO
。
ORA-6550 is a generic error for when your SQL doesn't compile. ORA-6550是 SQL无法编译时的一般错误 。 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: 该错误报告它认为问题是没有
INTO
子句,因为它认为您正在尝试执行SELECT INTO语句,如下所示:
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
. 我没有要进行测试的Oracle安装,但是我经历过Oracle 9i / 10g对CASE语句很奇怪,有时需要你使用
END CASE
而不是END
。 END CASE
is mentioned in the Oracle documentation here . END CASE
在此处的Oracle文档中提到 。
Usually you'd use DUAL for this sort of thing, testing if the variable is set: 通常你会在这种情况下使用DUAL,测试是否设置了变量:
var_type := 'B';
SELECT CASE var_type
WHEN 'B' then 'Beans'
WHEN 'L' then 'Legumes'
END CASE
FROM DUAL;
As answered before the errors says that you have to put the result of the select into a variable. 如前所述,错误表明您必须将select的结果放入变量中。
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. 如果表tableOfBeans包含多行,则会遇到另一个错误ORA-01422:exact fetch返回多行。 Use pseudo table dual instead or following pl/sql code.
使用伪表替代或替代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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.