返回布尔值的函数因“表达式类型错误”而失败

发布于 2024-10-21 03:14:20 字数 941 浏览 10 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(3

黑凤梨 2024-10-28 03:14:20

纯 SQL 无法识别布尔类型(直到版本 23c),但 PL/SQL 可以识别。所以你的查询不知道这个函数返回的数据类型。

该函数可以工作,所以你可以在另一个 pl/sql 块中使用

declare
myvar boolean;
begin
   myvar := compt_tree_profile_q.legal_user(1,1);
end;

但是你不能在纯 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

declare
myvar boolean;
begin
   myvar := compt_tree_profile_q.legal_user(1,1);
end;

But you can't use this function in a pure select statement.

晨光如昨 2024-10-28 03:14:20

您的函数返回一个布尔值。该数据类型对于 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.

爱的十字路口 2024-10-28 03:14:20

鉴于您在 SQL 中调用此函数,您可以使用内置的 SIGN 函数,而不是自行编写。

该函数将返回 -1、0 或 1,具体取决于参数的符号(分别为负、零或正)。

下面介绍了如何使用它:

SIGN(level_existance*types_with_impel)

以及如何将其放入 CASE 语句中:

SELECT CASE WHEN (SIGN(level_existance*types_with_impel) = 1)
            THEN 'TRUE'
            ELSE 'FALSE'
       END legal_user
FROM ...

在本例中,我只是返回一个字符串(“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:

SIGN(level_existance*types_with_impel)

And how you would work it into a CASE statement:

SELECT CASE WHEN (SIGN(level_existance*types_with_impel) = 1)
            THEN 'TRUE'
            ELSE 'FALSE'
       END legal_user
FROM ...

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

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