tags:

views:

24

answers:

1

I know for more than one result, you don't have much choice for using out parameters, but I'm wondering whats the difference between the following 2 cases where there's one out parameter.

Say I have two stored procedures:

CREATE PROCEDURE no_out_params(IN foo INT)
BEGIN
  SELECT foo + 1 as result;
END

and

CREATE PROCDURE with_out_params(IN foo INT, OUT result INT)
BEGIN
  SET result = foo + 1;
END

Then using JDBC I can either:

CallableStatement c1 = conn.prepareCall("{ call no_out_params(?)}");
c1.setInt(1, 5);
ResultSet rs = c1.executeQuery();
if (rs.next()) {
  return rs.getInt("result"); // or rs.getInt(1)
}

or

CallableStatement c1 = conn.prepareCall("{ call with_out_params(?, ?)}");
c1.setInt(1, 5);
c1.registerOutParameter(2, Types.INT);
c1.executeQuery();
return c1.getInt(2);

Assuming you're properly closing everything in try/finally's (omitted for brevity), does the non-out parameter method provide any advantage? If not, is it reasonable to assume that one should always use out parameters (if only to to be consistent in cases where there's more than one result)?

+1  A: 

Assuming you're properly closing everything in try/finally's (omitted for brevity), does the non-out parameter method provide any advantage?

The advantage is being able to retrieve the result set (in any language, Java/etc) without having to define all the OUT parameters (extra work for no value).

If you're only returning one value, you might want to see if a FUNCTION would work instead.

OMG Ponies
Unfortunately my particular case requires transactions with rollback so I think that rules out using a FUNCTION. I did notice, in a somewhat related but different issue, that if you call a stored procedure from within a stored procedure you need an out parameter (I think, I believe you could also use temporary tables).
cfairles