Skip to Main Content
  • Questions
  • Use DBMS_OUTPUT or HTP depending call origin

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Carlos.

Asked: June 12, 2018 - 7:12 pm UTC

Last updated: June 14, 2018 - 1:10 am UTC

Version: 12.1

Viewed 1000+ times

You Asked

Hey,
I have this procedure:

procedure output(i_msg in varchar2)
is
begin
  dbms_output.put_line(i_msg);
  -- if call is from apex i want to use htp.p
end;


Is it possible to switch the "output chanel" to htp when the caller is an apex application? I don't want to use a extra parameter in my procedure to make the switch. Just asking you how to determine if the call is from apex or from "normal" Sql Developer/Sql Plus etc.

Thanks,
Carlos

and Connor said...

Couple of options you could look at:

1) Is the Apex application user set

procedure output(i_msg in varchar2)
is
begin
  if v('APP_USER') is null then
    dbms_output.put_line(i_msg);
  else
    htp.p(i_msg);
  end if;
end;


2) Is this a PLSQL web based application (even if it is not apex)

procedure output(i_msg in varchar2)
is
  dummy varchar2(100);
begin
  dummy := owa_util.GET_CGI_ENV('REMOTE_ADDR');
  htp.p(i_msg);
exception
  when others then 
        dbms_output.put_line(i_msg);
end;


Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Carlos, June 13, 2018 - 6:36 pm UTC

The second options looks like the solution. The first one will not compile on enivronments where apex is not installed (because v function isn't available). But the second should work always the way i want...

Thanks a lot for your answer,
Carlos
Connor McDonald
June 14, 2018 - 1:10 am UTC

I got this useful tip from John Scott as well

"If that routine is called a lot, I’d use sys_context('APEX$SESSION', 'APP_USER') rather than V() for performance reasons. Also helps if it’s possible that APEX isn’t installed (hence no V function)."

which might help you out

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library