Question

I was wondering if anyone knew if it was possible to have an externally described variable in a stored procedure for DB2/400?

We have quite a few stored procedures out there, and they all access a certain file. We recently went through a field expansion and V7 is throwing an SQLSTATE 1004 (variable size difference). I now need to go out and change all the procedures that reference this file and use the expanded field size for the internal variable.

Ideally, I would like to change the procedures to allow an external reference for this definition, so in the future, all I have to do is re-compile this procedure. Is this possible in DB2?

Some more clarity:

function getItemCost(inStore,inItem)

declare storeVendor numeric(7,0); select vendor into storeVendor from vendor_table where store_number = inStore);

{other logic}

If we expand the vendor length from 7 to 10, I would need to change this code.

Was it helpful?

Solution

No. The SQL CREATE PROCEDURE statement doesn't allow 'like(somecolumn)'. But that's a good thing, because that interface is a contract between the stored procedure and all of its callers. If that interface could change dynamically, how would the callers know how to allocate memory for the variables? Ultimately, if the underlying column size changes, you need to find the stored procedures using that column and all of the callers of that stored procedure and change them all together. Imagine for a moment that one of those callers puts the column up on a 5250 display, or prints it on a report - you need to make room for the newly enlarged column.

One way to make that burden lighter is to create a new stored procedure (GetCustomerAmountDue_v2) which uses the new, longer column definition. Then any caller which needs that longer definition can be changed to call the _v2 procedure.

If your column is a character column, then it's possible for you to return a VARCHAR instead of a CHAR; the interface doesn't need to change because the contract already allows for a varying number of characters to be returned.

OTHER TIPS

Assuming IBM i 6.1 or later, you should be able to use CREATE TYPE to define your variable definitions as a User Defined Type (UDT). If you use your UDT to define table columns, the tables will need to be dropped and recreated if you drop and recreate the UDT. But somehow SQL procedures seem to get along fine after a UDT is recreated.

You possibly would do something like:

CREATE TYPE sqlexample/decN_M
AS decimal(7,2)

You'd declare your stored proc variable as decN_M instead of as DECIMAL(7,2). At some later date, you might run:

DROP TYPE sqlexample/decN_M ;
CREATE TYPE sqlexample/decN_M
AS decimal(10,2)

When you run your proc after that, it should handle the value as DECIMAL(10,2). Some testing should let you know quickly if this could work for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top