Use of boolean in PL/SQL

后端 未结 2 1579
眼角桃花
眼角桃花 2020-12-16 19:34

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

CREATE OR REPLACE FUNCTION checkEmpNo(eno numeric)
RETUR         


        
相关标签:
2条回答
  • 2020-12-16 20:05

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

    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.

    The ORA-01422 error is a completely separate issue. The function checkEmpNo includes the SELECT INTO statement

    SELECT emp_id 
      INTO emp_number
      FROM emp;
    

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

    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;
    
    0 讨论(0)
  • 2020-12-16 20:10

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

    dbms_output.put_line(diutil.bool_to_int(p_your_boolean));
    
    0 讨论(0)
提交回复
热议问题