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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5260238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 03:08:54  来源:igfitidea点击:

Function returning boolean fails on "expression is of wrong type"

oracleplsqloracle11gora-06553

提问by yoav.str

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 :

我正在使用 oracle 11g,但我无法理解我的问题出在哪里。我做了更困难的事情,但在过去的 5 小时里我在这件简单的事情上失败了:

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

逻辑 AND 等价于

         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

ORA-06552: PL/SQL: 语句被忽略 ORA-06553: PLS-382: 表达式类型错误 06552. 00000 - "PL/SQL: %s" *原因:
*操作: 行错误: 1 列: 7



This is how I run it in my IDE

这就是我在 IDE 中运行它的方式

 SELECT compt_tree_profile_q.legal_user(1,1)
 FROM dual 

回答by Michael Broughton

Pure SQL does not recognize a boolean type, although PL/SQL does. So your query does not know what datatype this function is returning..

纯 SQL 不识别布尔类型,尽管 PL/SQL 可以。所以你的查询不知道这个函数返回的是什么数据类型..

The function works, so you could in another pl/sql block use

该函数有效,因此您可以在另一个 pl/sql 块中使用

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.

但是不能在纯 select 语句中使用此函数。

回答by Rob van Wijk

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

您的函数返回一个布尔值。PL/SQL 知道此数据类型,但您使用的是 SQL 查询。SQL 不知道如何处理布尔值并说“表达式类型错误”。

Regards,
Rob.

问候,
罗布。

回答by Bernard

Given that you are calling this within SQL, you could use the built-in SIGN function instead of rolling your own.

鉴于您在 SQL 中调用它,您可以使用内置的 SIGN 函数而不是滚动您自己的函数。

The function will return -1, 0 or 1, depending on the sign of the parameter (negative, zero or positive respectively).

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

Here's how you would use it:

以下是您将如何使用它:

SIGN(level_existance*types_with_impel)

And how you would work it into a CASE statement:

以及如何将其处理为 CASE 语句:

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

在这种情况下,我只是返回一个字符串('TRUE' 或 'FALSE'),但您可以返回 SELECT 语句中有效的任何内容(列、SYSDATE 等)。