oracle PL/SQL 中布尔值的使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13560040/
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 04:40:39  来源:igfitidea点击:

Use of boolean in PL/SQL

oracleplsql

提问by Chandeep

I have a function in PL/SQL which checks if a particular emp_idexists or not which is:

我在 PL/SQL 中有一个函数,它检查一个特定的是否emp_id存在,它是:

CREATE OR REPLACE FUNCTION checkEmpNo(eno numeric)
RETURN boolean IS
    emp_number number;
BEGIN
    SELECT emp_id INTO emp_number
    FROM emp;
    IF eno=emp_number
    THEN
        return true;
    ELSE
        return false;
    END IF;
END checkEmpNo;

The function compiles successfully, but when I try to run it as:

该函数编译成功,但是当我尝试将其运行为:

DECLARE
    exist boolean;
BEGIN
    exist=checkEmpNo(1);
    dbms_output.put_line(exist);
END;

it returns the error:

它返回错误:

ORA-06550: line 5, column 1:
PLS-00306: wrong number or types of arguments in call to 'PUT_LINE'
ORA-06550: line 5, column 1:
PL/SQL: Statement ignored

3. BEGIN
4. exist:=checkEmpNo(1);
5. dbms_output.put_line(exist);
6. END;

EDIT:

编辑:

I also tried this:

我也试过这个:

DECLARE
    exist boolean:=true;
BEGIN
    if(exist=checkEmpNo(1))
    then
        dbms_output.put_line('true');
    else
        dbms_output.put_line('false');
    end if;
END;

And it returns the error: ORA-01422: exact fetch returns more than requested number of rows

它返回错误: ORA-01422: exact fetch returns more than requested number of rows

回答by Justin Cave

dbms_output.put_lineis not overloaded to accept a boolean argument. You can do something like

dbms_output.put_line不会重载以接受布尔参数。你可以做类似的事情

dbms_output.put_line( case when exist = true 
                           then 'true'
                           else 'false'
                        end );

to convert the boolean into a string that you can then pass to dbms_output.

将布尔值转换为字符串,然后您可以将其传递给dbms_output.

The ORA-01422 error is a completely separate issue. The function checkEmpNoincludes the SELECT INTOstatement

ORA-01422 错误是一个完全独立的问题。该函数checkEmpNo包括SELECT INTO语句

SELECT emp_id 
  INTO emp_number
  FROM emp;

A SELECT INTOwill generate an error if the query returns anything other than 1 row. In this case, if there are multiple rows in the emptable, you'll get an error. My guess is that you would want your function to do something like

SELECT INTO如果查询返回除 1 行以外的任何内容,A将生成错误。在这种情况下,如果emp表中有多行,您将收到错误消息。我的猜测是你希望你的函数做类似的事情

CREATE OR REPLACE FUNCTION checkEmpNo(p_eno number)
  RETURN boolean 
IS
  l_count number;
BEGIN
  SELECT count(*)
    INTO l_count
    FROM emp
   WHERE emp_id = p_eno;

  IF( l_count = 0 )
  THEN
    RETURN false;
  ELSE
    RETURN true;
  END IF;
END checkEmpNo;

回答by Rene

Alternatively you can use the Oracle function diutil.bool_to_intto convert a boolean value to an integer: True -> 1, False -> 0.

或者,您可以使用 Oracle 函数diutil.bool_to_int将布尔值转换为整数:True -> 1, False -> 0。

dbms_output.put_line(diutil.bool_to_int(p_your_boolean));