I have a db2 stored procedure that takes in some parameters, gets some data from somewhere and then returns a result set through a cursor.
Now I want to write a table function in db2, that will call this stored procedure, read from the result set and return the data in the result set as a table (eventually I want to use this table function in a join).
I would like to know if this is permitted in db2 (we're using DB2 v10.5), i.e. execute a stored procedure in a table function and fetch and read from the result set from the stored procedure.
If so, what is the right syntax for calling the stored procedure and reading the result set inside a table function in db2?
Thanks!
Best Answer
What you want to accomplish is certainly possible by using a pipelined table function, but it isn't necessarily a wise thing to do. For any non-trivial result set row-based processing is always significantly slower than set-based processing. In addition to that, by hiding your data access logic in a table function you deprive the query optimizer of any information about the statistics and other physical characteristics of the underlying tables and indexes. As a result, performance of such a chimera will lie anywhere between suboptimal and unacceptable.
Consider rethinking your approach; in most cases you should be able to implement your data retrieval and manipulation logic in a single query, may be wrapped in a view for convenience.
If you, however, insist on shooting yourself and those who come after you in your collective feet, here's how you might go about doing that.
Footnotes:
The
READS SQL DATA
here is important; a table function cannot have side effects, and so shouldn't the stored procedure it calls. We enforce this contract at compilation time.Make sure Db2 knows we want data in the caller, otherwise it will optimize it away.
Then the function itself:
and call it:
The
CARDINALITY
clause lets you tell the optimizer how many rows you expect the function to return, but it may not take this information into account, and even if it does, it may not help.