Stored procedures can also be used as a mechanism to return complex query results. Using a named query to invoke a stored procedure is very similar to a direct SQL named query, with one minor addition. The <sql> tag for a stored procedure uses an attribute named stored-procedure. When using a stored-procedure, this attribute must be set to true. Here is an example of how you might define a stored procedure in a repository definition file. Note that the values used in the <returns> and <dependencies> tags in this example are dependent upon what the stored procedure returns and defines constraints for.

<item-descriptor name=...>
      <sql stored-procedure="true">
         { call myStoredProcedure (?, ?) }

The syntax for calling a stored procedure is different from executing an SQL statement. The body of the <sql> tag for a stored procedure must use the syntax required by java.sql.CallableStatement. There are two formats typically used in a CallableStatement, one for procedures, which do not have an explicit return value, and one for functions, which do. Unless you use Oracle, stored procedures in the SQL repository must use the procedure form, as in the above example. The enclosing curly braces are required. The question marks indicate parameters. In stored procedures, parameters can be IN, OUT and INOUT, indicating that the values go in the database, come out, or both go in and come out. The SQL repository supports only IN parameters. Any OUT parameters or INOUT parameters will not function correctly, except in the case of Oracle stored procedures.

Any stored procedure used must return a java.sql.ResultSet. Most JDBC drivers do so by default, but there are exceptions. Oracle, for example, requires some special tuning, as described in the next section.

loading table of contents...