Callable Statement Syntax

Advertisement
I am using the callable statement to invoke a MS SQL Server stored procedure that will return a result set. I have several questions:
1) According to the Java documentation I should be using the following format for a prepareCall that will return a result paramater with 4 input paramaters:
{ ? = call SP_ABC(?, ?, ?, ?)}
In my particular case the stored procedure will return 14 pieces of data in a result set. Do I need to write code for 14 registerOutParameter fields? Is the syntax mentioned above accurate for the 14 pieces of data or do I need to repeat the question marks (?) 14 times? What shoud the syntax be for my prepareCall statement?
Advertisement

Replay

The ? in the sql syntax refer to variables passed to the procedure as either in or in/out parameters. You need to write code to register any OUT variables (including any return value).
The data returned from the SP will be returned as a ResultSet which is processed in the normal way.
So assuming that you have a procedure declared as:
create proc p @1 int, @2 int OUTPUT as ...then your code would look like this:
statement = connection.prepareCall("{?=call p (?,?}");
// register the return code
statement.registerOutParameter(1,Types.INTEGER);
// register the OUTPUT var @2
statement.registerOutParameter(3,Types.INTEGER);
// Set the input param @1
statement.setInt(2,0);
rs = statement.executeQuery();
// Get the return code
int rc = statement.getInt(1);
// Get the output param @2
int out = statement.getInt(3);
// process the result set
while(rs.next()) {...}Dave