0

I'm running below code snippet, where dbms_ouput is not printing anything and just showing function compiled successfully in SQL Developer.

set serveroutput on size 30000;
create or replace function check_status (p_user_name in varchar2) return number is
begin
DBMS_OUTPUT.PUT_LINE('Yoo');
end;
/

where below code snippet is printing. I wounder why its not printing inside function.

set serveroutput on size 30000;
begin
DBMS_OUTPUT.PUT_LINE('Yoo');
end;
/
2
  • 1
    Did you only create the function or did you call it too? IN what you've shown, the code in the function is only compiled, it isn't executed - and the output won't be produced until it is executed, by calling the function.You need to have set serveroutput on before calling the function too. But as there is no return in the function, it will error at runtime anyway... Commented Nov 2, 2018 at 18:26
  • 1
    In your first snippet you create a function but never call it. Commented Nov 2, 2018 at 18:26

1 Answer 1

2

You have compiled the function, but you haven't called it. The code in the function will only be run when the function is called, so it is correct for it to not show any output at compile time.

Your function also needs to return a value; at the moment calling it would get:

select check_status('test') from dual;

ORA-06503: PL/SQL: Function returned without value
ORA-06512: at "MYSCHEMA.CHECK_STATUS", line 4

Yoo

which does show the output, but also errors.

So your function needs to return something:

create or replace function check_status (p_user_name in varchar2) return number is
begin
  DBMS_OUTPUT.PUT_LINE('Yoo');
  return 42;
end;
/

Function CHECK_STATUS compiled

select check_status('test') from dual;

CHECK_STATUS('TEST')
--------------------
                  42

Yoo

Also note that anyone calling the function will only see that Yoo if they have enabled output in their session or client. As you can't control that you shouldn't rely on it. It's mostly useful for debugging.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.