简体   繁体   中英

Oracle 12c Function: value returned correctly but not shown in select statement

I have an Oracle function:

CREATE OR REPLACE FUNCTION "MYFUNC"
RETURN VARCHAR2
AS  
        lv_result VARCHAR2(50);
BEGIN               
        SELECT 'A' INTO lv_result FROM DUAL;
        DBMS_OUTPUT.PUT_LINE(lv_result);
        RETURN lv_result;
END;

This function simply returns the 'A' value.

If I call the function:

SELECT MYFUNC() FROM DUAL;

It shows me no value, just blank.

If I check the value:

SELECT
CASE    
WHEN
    MYFUNC() = 'A' THEN
    'OK' ELSE 'NO' 
END AS RES
FROM DUAL;

then the result is OK, so the value is returned correctly.

Before returning value, the 'A' value is correctly printed to DBMS_OUTPUT.

Oracle version is 12c. I tested also in Oracle 11g and it works, so it seems to be just an Oracle 12c issue.

Any ideas why this happens and how to print that value using the select statement?

Which 10g is it? Works OK for me on 10.2.0.5.0.

SQL> select * From v$version;

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi
PL/SQL Release 10.2.0.5.0 - Production
CORE    10.2.0.5.0      Production
TNS for Linux: Version 10.2.0.5.0 - Production
NLSRTL Version 10.2.0.5.0 - Production

SQL> CREATE OR REPLACE FUNCTION "MYFUNC"
  2  RETURN VARCHAR2
  3  AS
  4          lv_result VARCHAR2(50);
  5  BEGIN
  6          SELECT 'A' INTO lv_result FROM DUAL;
  7          DBMS_OUTPUT.PUT_LINE(lv_result);
  8          RETURN lv_result;
  9  END;
 10  /

Function created.

SQL> select myfunc() from dual;

MYFUNC()
-------------------------------------------------------------------
A

SQL>

Tried on 10.2.0.4.0 - works OK as well. I don't think I have other 10g databases available ...

Works on 12.2.0.1.0 too; that's the highest version I have.

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