Calling a DB2 Stored Procedure in a DB2 function - db2

Can we call a DB2 Stored procedure from a DB2 function. Also my stored procedure has parameters and it returns a table, so I have to pass these parameters to the stored procedure through the function only.
Can anyone let me know if its possible.
Thanks much

I don't see any reason why it wouldn't work. Nothing in the documentation is sticking out to me that says User Defined Functions are prevented from, or shouldn't be calling a Stored Procedure.
It seems like the easy answer would be to try it out by creating a small test scenario. Without knowing why you're looking to do this, I can't offer much more advice since I've never done it personally. However another option, if you can't get the UDF to process result sets from a stored procedure call is to have the UDF call a program (or service program procedure) and have that object call the stored procedure and return whatever info you need the UDF to return.

Related

How to determine which stored procedure called another procedure?

I have some procedures that calls another procedure with different parameters. Is there a way in that procedure to know who called it? Some kind of stacktrace...
I would recommend finding a cleaner way to solve what are you trying to solve, like passing extra parameter to identify what you need to.
Anyway you can use MON$CALL_STACK to get, probably, what you're looking for.

Finding "types" (reporter/command, and different contexts) of Procedures

I am trying to extract, and sort, a list of all procedures in a model. Basically I want only the procedures that can be run in the Observer context, and only commands, not reporters. Workspaces have a .getProcedures():Map<String, Procedure> method and I am accessing the Procedure objects from that.
This leads me to two related questions: 1. Is there a way for me to find out if a Procedure object is a reporter or a command? The NetLogo desktop version seems to be able to make this distinction, so I think the answer might be yes. And 2. is there a way for me to find out what the Context of a procedure is, i.e. whether it is a patch/turtle/link procedure or an observer procedure?
http://ccl.northwestern.edu/netlogo/docs/scaladoc/org/nlogo/nvm/Procedure.html shows that Procedure has a syntax method returning an api.Syntax object. The methods you need are there, namely ret (which will be Syntax.VoidType or Syntax.WildcardType according to whether it is a command or reporter procedure) and agentClassString (which might be e.g. "OTPL").
Oh actually I see also now that the first constructor parameter to Procedure is Type tyype (the extra y is because type is a keyword in Scala), so for the first part of your question, you could equally well check that and see whether it is COMMAND or REPORTER. There probably isn't any good reason the information is redundantly stored.

how to use records as parameters in stored function

I want to use record type as parameter but I got message that function cannot have record type parameters. I have a Dao function which perform various operation on a Arraylist passed through parameter and I need to implement it in stored procedure. So any help will be greatly appreciated. thanks!
The function m looking for is something like:
CREATE OR REPLACE FUNCTION est_fn_get_emp_report(rec record,...)
I am new using postgresql but have used stored functions before but never have to use record type parameters.
The simple issue is you can't specify a record. You can specify some polymorphic types (ANYARRAY, ANYELEMENT) as an input of a function but it needs to have a structure known at planning time and this can lead to issues with polymorphic types as input args on even on a good day. The problem with a record is that PostgreSQL wont necessarily know what the internal structure is when it is passed in. ROW(1, 'test') is not useful in a functional context.
Instead you want to specify complex types. You can actually take this very far in terms of relying on PostgreSQL. This allows you to specify a specific type of record when passing it in.

variable table or column names in a function

I'm trying to search all tables and columns in a database, a la here. The suggested technique is to construct SQL query strings and then EXEC them. This works well, as a stored procedure. (Another example of variable table/column names is here. Again, EXEC is used to execute "dynamic SQL".)
However, my app requires that I do this in a function, not an SP. (Our development framework has trouble obtaining results from an SP.) But in a function, at least on SQL Server 2008 R2, you can't use EXEC; I get this error:
Invalid use of a side-effecting operator 'INSERT EXEC' within a function.
According to the answer to this post, apparently by a Microsoft developer, this is by design; it has nothing to do with the INSERT, only the fact that when you execute dynamically-constructed SQL code, the parser cannot guarantee a lack of side effects. Therefore it won't allow you to create such a function.
So... is there any way to iterate over many tables/columns within a function?
I see from BOL that
The following statements are valid in a function: ...
EXECUTE
statements calling extended stored procedures.
Huh - How could extended SP's be guaranteed side-effect free?
But that doesn't help me anyway:
The extended stored procedure, when it is called from inside a
function, cannot return result sets to the client. Any ODS APIs that
return result sets to the client will return FAIL. The extended stored
procedure could connect back to an instance of SQL Server; however, it
should not try to join the same transaction as the function that
invoked the extended stored procedure.
Since we need the function to return the results of the search, an ESP won't help.
I don't really want to get into extended SP's anyway: incrementing the number of programming languages in the environment would complicate our development environment more than it's worth.
I can think of a few solutions right now, none of which is very satisfactory:
First call an SP that produces the needed data and puts it in a table, then select from the function which merely reads the result from the table; this could be trouble if the search takes a while and two users' searches overlap. Or,
Have the application (not the function) generate a long query naming every table and column name from the db. I wonder if the JDBC driver can handle a query that long. Or,
Have the application (not the function) generate a long series of short queries naming every table and column name from the db. This will make the overall search a lot slower.
Thanks for any suggestions.
P.S. Upon further searching, I stumbled across this question which is closely related. It has no answers.
Update: No longer needed
I think this question is still valid, and we may again have a situation where we need it. However, I don't need an answer anymore for the present problem. After much trial-and-error I managed to get our application framework to retrieve row results from the RDBMS via the JDBC driver from the stored procedure. Therefore getting the thing to work as a function is unnecessary.
But if anyone posts an answer here that helps with the stated problem, I will be happy to upvote and/or accept it as appropriate.
An sp is basically a predefined sql statment with some add ons.
So if you had
PSEUDOCODE
Create SP_DoSomething As
Select * From MyTable
END
And you can't use the SP
Then you just execute the SQL as in "Select * From MyTable"
As for that naff sql code.
For start you could join table to column with a where clause, which would get rid of that line by line if stuff.
Ask another question. Like How could this be improved, there's lots of scope for more attempts than mine.

stored procedures as queries: CallableStatement vs. PreparedStatement

PostgreSQL documentation recommends using a CallableStatement to call stored procedures.
In the case of a stored procedure that returns a rowset, what are the differences between using CallableStatement:
String callString = "{ call rankFoos(?, ?) }";
CallableStatement callableStatement = con.prepareCall(callString);
callableStatement.setString(1, fooCategory);
callableStatement.setInt(2, minimumRank);
ResultSet results = statement.executeQuery();
And using a regular PreparedStatement:
String queryString = "SELECT FooUID, Rank FROM rankFoos(?, ?);";
PreparedStatement preparedStatement = connection.prepareStatement(queryString);
preparedStatement.setString(1, fooCategory);
preparedStatement.setInt(2, minimumRank);
ResultSet results = statement.executeQuery();
As I understand, CallableStatement offers a language-agnostic way of calling stored procedures. This doesn't matter to me though, since I know I'm using PostgreSQL. As far as I can see, the obvious advantage of using the PreparedStatement is a more versatile query, treating the stored procedure as a table, on which I can use WHERE, JOIN, ORDER BY, etc.
Are there aspects or differences between the methods that I'm missing? In the case of a stored procedure used as a query, which is recommended?
I'm pretty sure the second approach does not work at all with some RDBMS's, but since you are only going to use PostgreSQL, that shouldn't matter too much. For your simple case, there really isn't much of a downside. There are two issues I can see popping up:
Depending on how the stored procedures are written, they may require you to register out parameters in order to execute the procedure. That's not going to be possible at all with prepared statements. If you control both the creation of the stored procedure and the calling code, you probably don't have to worry about this.
It limits the effectiveness of calling stored procedures in the first place. One of the main advantages of a stored procedure is to encapsulate the querying logic at the database level. This allows you to tune the query or in some cases add functionality without having to make changes to your code. If you're planning on adding where clauses joins to the results of the stored procedure call, why not just put the original query in your java layer?
The main difference is independence and encapsulated way of programming.
Imagine that you are a Java Programmer and you don't know how to use Database, in this way you can not use the second method and u will meet problems in the second way.
First method allows you to do your java code, and ask somebody to write the quires for you as Stored-Procedure, so u can use them easily.
I do agree with #dlawrence