Using a SP in a DB2 Function

pvsbandi
pvsbandi used Ask the Experts™
on
Hi,

  I have a stored procedure  SP1 which returns the following result set.

ID1                  CL_ID                Link_ID1
123                   ABC                    234
 333                  DEF                       --
444                   HIJ                      567

Now, i want to create a UDF with V_ID as a parameter.
This will call this stored Procedure SP1 and pass the V_ID parameter against the result set, to filter for the Link_ID1.

So, if i pass the V_ID as 444 inside the UDF, then it should check for the Link_ID1 in the stored procedure. Since there is no match, it should return 444.
 IF i pass 234 in the UDF, then it should look for the match of 234 and return ID1, which is 123.

Is there a way i can do it? Please advise.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi pvsbandi,

In DB2, a stored procedure can call a UDF (or anything else), but a UDF cannot call a stored procedure.  

Can you create another stored procedure that calls the one that you have now?


Kent

Author

Commented:
Sure.. how? Please advise.

  I want to be able to use the function in the select statements  to return the ID1 or the passed V_ID from the function.
can you post your stored procedure code? (the one that generates that table)
maybe you can use only a function to get it all done, maybe a view will be enough
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Author

Commented:
Hi,

 Please find the code attached.
SP.sql
>  a UDF cannot call a stored procedure

This depends on the DB2 platform and version, I believe. It's possible in DB2 LUW since version 8.2 if I'm not mistaken.

There are certain limitations, of course. Particularly, you cannot readily process cursors in SQL UDFs.

Author

Commented:
Thanks. Can this procedure be altered to facilitate the creation of a UDF?
Like I said, you won't be able to process a cursor in the UDF, so the only way to alter your SP is to remove the OPEN statement, which is probably not what you want.

Can you explain in simple terms what you're trying to achieve in your stored procedure?
A call to a procedure should be okay in reasonably recent releases, though the procs shouldn't include statements such as CONNECT, COMMIT, etc. I'm not sure if DB2 9.7 provides a way to handle it. Cursors have been possible with DB2 for AS/400s for a number of years, but I've only seen them defined within the functions themselves.

If a cursor cannot be declared in a function for a stored proc result set, maybe a second proc can do it. The function might then be a wrapper for proc2. Maybe.

Tom

Author

Commented:
Sorry for the delay..was out of town.
   and thanks for the advise! I have created a new procedure with the logic embedded in it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial