Wednesday, October 19, 2011

To V or not to V...

Earlier today, I tweeted the following:
After this morning, I don't think I will 
ever use the "v" function again. #orclapex
I wanted to qualify what I meant by that, since sometimes you only see one side of the conversation on Twitter. Also, it's been a while since my last post, so this give me the opportunity to remedy that as well. The APEX "v" function works, and works quite well. For those who have not used it, the "v" function is an APEX-specific function that when you pass an APEX item to it, it will return the value of that item for a specific user session. What's cool about it is that it also works from named PL/SQL program units, as long as they were initiated from an APEX session. Thus, you can write a PL/SQL package that takes in few, if any parameters and still can refer to items that are set in the APEX session state via the "v" function:
PROCEDURE foo
IS
  l_customer_name   VARCHAR2(255) := v('P1_CUSTOMER_NAME');
BEGIN
...
END;
/
The specific issue that I had was that I did used the "v" function in quite a few places across a suite of PL/SQL packages. It cut down on what I needed to pass from package to package, and even allowed me to omit some procedures from the package specification. It worked magnificently. That is, until I tried to call one of the packages from SQL*Plus. Since there is no APEX session context set in SQL*Plus - and even if there was, which you can do, the items that I required to be set would not be - my package failed spectacularly. Thus, I had to go back through several packages and retro-fit them to be APEX-agnostic and remove all traces of the "v" function in favor of parameters. The lesson to learn from this is simple: take some time to consider whether or not you think a block of code will ever be called from outside of APEX. Even if there is a remote chance that it will, it may pay off big time later if you choose to make that code APEX-agnostic and rely on parameters instead.

4 comments:

dmcghan said...

Hi Scott,

There's another really good reason to use parameters over the v function. If you change the name of an item in the application, the v function will reference the wrong item name until the offending code is found and updated. But how do you find the code that needs to be fixed?

Using parameter passing from APEX to a package leaves little "artifacts" in your application and the APEX Advisor can pick up on these artifacts and tell you to fix them.

Regards,
Dan

Scott said...

Very true - there's lot of reasons for parameters vs. the "v" function. But you have to admit that the "v" function is the quickest & easiest way to do things - as limiting in the long term as it may be...

- Scott -

Ronald Hollak said...

Hi Scott,

Another place where I had to use the V function in a stored procedure was in my generic errorhandler.
You want your errorhandling functions to have some application-context, so what I did was wrapping the V (and NV) function:

create function apex_V(p_item_name in varchar2)
return varchar2
is
e_no_apex exception;
pragma exception_init (e_no_apex, -904);
l_return varchar2(255); begin
execute immediate 'select v(:P_ITEM_NAME) from dual' into l_return using p_item_name;
return l_return;
exception
when e_no_apex then
return null;
end apex_V;

In this case I return null when not in Apex, but you get the idea.

Gr.
Ronald

Shahana said...

Here in your example what is V function doing?