This is a one-on-one copy of an item blogged by Scott Splendolini

<COPY>

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.

</COPY>

Leave a Reply