简体   繁体   中英

How to display value return from function in procedure

I want to pass in p_string to the procedure, then the procedure will pass it to the function, and the function will return boolean to the procedure, the boolean return to procedure will then display using dbms_output.put_line. However, i have been having problem displaying it. How should i display it?

CREATE OR REPLACE PACKAGE BODY LAB4_527802_pkg AS
FUNCTION LAB4_527802_FCN( p_string VARCHAR2)
  RETURN BOOLEAN
  AS
  string1 BOOLEAN;
    BEGIN
      IF p_string = 'AAA' THEN
          string1 := TRUE;
        ELSE
          string1 := FALSE;
      END IF;
      return string1;
  END LAB4_527802_FCN;

  PROCEDURE LAB4_527802_PROC (p_string varchar2)
  AS
  string1 boolean;
  BEGIN
  string1 := LAB4_527802_pkg.LAB4_527802_FCN(p_string);
  dbms_output.put_line (string1);
  END LAB4_527802_PROC;
  END LAB4_527802_pkg;
  /
  SHOW ERRORS

This is how i call the procedure.

  set serveroutput on;  
  begin
  LAB4_527802_pkg.LAB4_527802_PROC ('AAA');
  end; 

Although named string1 the variable is declared as boolean, both in function LAB4_527802_FCN and in procedure LAB4_527802_PROC. I would first suggest using a different name. But as you may imagine, this is not the reason for the problem.

In line

dbms_output.put_line (string1);

you're trying to print a variable of type boolean which doesn't work. Procedure put_line needs a 'string' (char, varchar, varchar2) argument.

I suspect you get the following error, don't you?

PLS-00306: wrong number or types of arguments in call to 'PUT_LINE'

You need to convert the boolean to a string in order to print it. Eg:

DBMS_OUTPUT.put_line (CASE WHEN string1 THEN 'TRUE' ELSE 'FALSE' END);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM