Skip to main content

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.

Comments

Dan McGhan 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
Here in your example what is V function doing?

Popular posts from this blog

Custom Export to CSV

It's been a while since I've updated my blog. I've been quite busy lately, and just have not had the time that I used to. We're expecting our 1st child in just a few short weeks now, so most of my free time has been spent learning Lamaze breathing, making the weekly run to Babies R Us, and relocating my office from the larger room upstairs to the smaller one downstairs - which I do happen to like MUCH more than I had anticipated. I have everything I need within a short walk - a bathroom, beer fridge, and 52" HD TV. I only need to go upstairs to eat and sleep now, but alas, this will all change soon... Recently, I was asked if you could change the way Export to CSV in ApEx works. The short answer is, of course, no. But it's not too difficult to "roll your own" CSV export procedure. Why would you want to do this? Well, the customer's requirement was to manipulate some data when the Export link was clicked, and then export it to CSV in a forma

Refreshing PL/SQL Regions in APEX

If you've been using APEX long enough, you've probably used a PL/SQL Region to render some sort of HTML that the APEX built-in components simply can't handle. Perhaps a complex chart or region that has a lot of custom content and/or layout. While best practices may be to use an APEX component, or if not, build a plugin, we all know that sometimes reality doesn't give us that kind of time or flexibility. While the PL/SQL Region is quite powerful, it still lacks a key feature: the ability to be refreshed by a Dynamic Action. This is true even in APEX 5. Fortunately, there's a simple workaround that only requires a small change to your code: change your procedure to a function and call it from a Classic Report region. In changing your procedure to a function, you'll likely only need to make one type of change: converting and htp.prn calls to instead populate and return a variable at the end of the function. Most, if not all of the rest of the code can rem

Manipulating Images with the... Database?

A recent thread on the OTN HTML DB Forum asked about how to determine the width & height of an image stored as a BLOB in an Oracle table. I mentioned in that thread that I have some code to manipulate an image stored in a BLOB column. This is particularly useful if you’re going to let users upload images, and you want to re-size them to display as a thumbnail. Thanks to Oracle interMedia , it is trivial to manipulate the width, height, and other attributes of images stored in an Oracle table. I’ve created a sample application here which demonstrates Oracle interMedia and HTML DB in action. Feel free to have a look. You can download this application from HTML DB Studio as well. Basically, this application allows you to upload images and perform an operation on the image as it is inserted into the PHOTO_CATALOG table. There are two places where some PL/SQL code is required: an After Submit process on page 2, and a procedure to display the images. Here is the PL/SQL for the After