返回布尔值的函数因“表达式类型错误”而失败
我正在使用 oracle 11g,我只是不明白我的问题出在哪里。 我做了很多更困难的事情,但在过去的 5 小时里我在这个简单的事情上失败了:
这是函数体
FUNCTION legal_user(
level_existance number
,types_with_impel number)
RETURN BOOLEAN
IS
v_ret_val BOOLEAN;
BEGIN
v_ret_val := FALSE;
IF (level_existance*types_with_impel>0) then
v_ret_val := TRUE;
DBMS_OUTPUT.PUT_LINE('true');
else
DBMS_OUTPUT.PUT_LINE('false');
END IF;
return v_ret_val;
END legal_user;
这是规范:
FUNCTION legal_user(
level_existance number
,types_with_impel number)
RETURN BOOLEAN;
它的逻辑和等效于
A*B>0?true:false;
我收到的错误消息是
ORA-06552: PL/SQL : 声明被忽略 ORA-06553: PLS-382: 表达式类型错误 06552.00000 - “PL/SQL:%s” *原因:
*行动: 行错误:1 列:7
这就是我在 IDE 中运行它的方式
SELECT compt_tree_profile_q.legal_user(1,1)
FROM dual
I am using oracle 11g and I just cant under stand where my problem is.
I have made much more difficult stuff but I fail in this simple thing for the last 5 hr :
This is the function body
FUNCTION legal_user(
level_existance number
,types_with_impel number)
RETURN BOOLEAN
IS
v_ret_val BOOLEAN;
BEGIN
v_ret_val := FALSE;
IF (level_existance*types_with_impel>0) then
v_ret_val := TRUE;
DBMS_OUTPUT.PUT_LINE('true');
else
DBMS_OUTPUT.PUT_LINE('false');
END IF;
return v_ret_val;
END legal_user;
This is the spec :
FUNCTION legal_user(
level_existance number
,types_with_impel number)
RETURN BOOLEAN;
which does logical AND equivlant to
A*B>0?true:false;
The error message I am getting is
ORA-06552: PL/SQL: Statement ignored
ORA-06553: PLS-382: expression is of wrong type
06552. 00000 - "PL/SQL: %s"
*Cause:
*Action:
Error at Line: 1 Column: 7
This is how I run it in my IDE
SELECT compt_tree_profile_q.legal_user(1,1)
FROM dual
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
纯 SQL 无法识别布尔类型(直到版本 23c),但 PL/SQL 可以识别。所以你的查询不知道这个函数返回的数据类型。
该函数可以工作,所以你可以在另一个 pl/sql 块中使用
但是你不能在纯 select 语句中使用这个函数。
Pure SQL does not recognize a boolean type (until version 23c), although PL/SQL does. So your query does not know what datatype this function is returning..
The function works, so you could in another pl/sql block use
But you can't use this function in a pure select statement.
您的函数返回一个布尔值。该数据类型对于 PL/SQL 是已知的,但您正在使用 SQL 查询。 SQL 不知道如何处理布尔值并说“表达式类型错误”。
问候,
抢。
Your function returns a boolean. This datatype is known to PL/SQL, but you are using a SQL query. SQL doesn't know how to handle booleans and says "expression is of wrong type".
Regards,
Rob.
鉴于您在 SQL 中调用此函数,您可以使用内置的 SIGN 函数,而不是自行编写。
该函数将返回 -1、0 或 1,具体取决于参数的符号(分别为负、零或正)。
下面介绍了如何使用它:
以及如何将其放入 CASE 语句中:
在本例中,我只是返回一个字符串(“TRUE”或“FALSE”),但您可以返回 SELECT 语句中有效的任何内容(列、SYSDATE 等)。
Given that you are calling this within SQL, you could use the built-in SIGN function instead of rolling your own.
The function will return -1, 0 or 1, depending on the sign of the parameter (negative, zero or positive respectively).
Here's how you would use it:
And how you would work it into a CASE statement:
In this case, I'm just returning a string ('TRUE' or 'FALSE'), but you can return anything that's valid within your SELECT statement (a column, SYSDATE, etc).