#### Stored procedures

A stored procedure is a compiled program, consisting of one or more statements and is saved into a database. The statement in a stored procedure can be an SQL statement or a control flow statement, such as an If-else statement or a Loop statement. A stored procedure is stored in the DBMS, so that it can be called locally or remotely. In addition, a stored procedure can return a value, single result set or multiple result sets. Currently, JReport supports stored procedures that return a single result set. If a stored procedure returns more than one result set, the first one will be used by JReport.

As a program, a stored procedure can take parameters. There are three types of parameters: IN, OUT and INOUT. The IN parameter lets you pass values to the procedure. The OUT parameter returns values to the caller. The INOUT parameter enables you to pass initial values to the procedure, and then returns the updated value to the caller.

In addition, JReport Designer provides you with the Data Manager which allows you to control the data retrieval of your stored procedures that function as queries, including the number of rows to be displayed and the duration required for the retrieval. It can also keep access information from previous runs of a stored procedure (for details, see Data Manager).

##### Adding stored procedures

To add procedures stored in the database to a catalog via a JDBC connection, take the following steps:

1. In the Data tab of the Catalog Browser, right-click the Stored Procedures node of the JDBC connection, and then click Add Stored Procedure on the shortcut menu. The Add Stored Procedures dialog displays. See the dialog.

2. The dialog lists all the stored procedures in the Stored Procedures box in a three-level tree. The top level is SQL-catalog, the second is SQL-schema and the last level is stored procedures. Select the required stored procedures, and then click Add.
3. If any of the selected procedure contains parameters, the Stored Procedure Parameters dialog will appear for you to input the required values. These values will be saved inside the stored procedure object, and will be used as the default value when executing this procedure. You can edit the stored procedure parameters at any time. For details, see Editing parameter values.
4. When finished, click the Done button to close the dialog.

When a stored procedure has been added into a catalog, JReport will in turn do the following:

- Retrieve the stored procedure's information from DBMS via JDBC.
- Prompt you to provide the values of IN and INOUT parameters if the stored procedure has them.
- Execute the stored procedure once in order to get the result set.
- Create the field objects of the procedure object according to the result set returned.

You will then be able to use the field objects to design your reports.

Notes:

- Not all database management systems (DBMS) support database stored procedures. In such a case, you may see nothing in the Procedures box.
- When adding a stored procedure, you may get the following error:

  [DBS-B]Could not find stored procedure 'test1;1'.

  This is because test;1 becomes quoted when JReport Designer finds the semicolon in it. To remove the quotation marks, you will have to modify your connection information. To do this, right-click your connection and then select Modify to bring out the Get JDBC Connection Information dialog. Then in the Quote Qualifier tab, check the User Defined radio button, and remove all the characters from the Extra Characters and Quote Character text boxes.

- Due to the unique nature of Oracle stored procedures, you are unable to add them directly into a JReport Designer connection. Thus, the usage of JReport Designer is limited when you use an Oracle database. As an alternative, JReport has developed the user data source API which can use a stored procedure in Oracle. For details, see Oracle stored procedure UDS.

##### Editing parameter values

When you add a stored procedure, you will be asked to input values for its parameters. These values will then be saved inside the stored procedure object in the catalog, and will be used as the default values when executing the stored procedure. You can edit stored procedure parameters at any time. Also, the IN type parameter has been available for use with a stored procedure in a report.

To edit the parameter values of a stored procedure:

1. Right-click the stored procedure, then click Parameters on the shortcut menu to bring up the Stored Procedure Parameters dialog. See the dialog.

2. This dialog lists all the IN and INOUT parameters in the stored procedures. Double-click the Value cell to edit the value of each parameter.
3. Double-click the bindParameterName cell to bind the IN and INOUT parameters in the stored procedure to an existing catalog parameter with the same type. You can input the parameter name as @parametername. By default, the bound parameter will be the one with the same name of the IN and INOUT parameter which is automatically created when adding the stored procedure.
4. Click OK to apply the changes to the parameters.

##### Updating stored procedures

If you make any changes to stored procedures in the database, you will need to update them in the connection so that the reports built on them will work properly. To do this:

1. Select any stored procedure, right-click it, and click Update on the shortcut menu.
2. In the Add Stored Procedures dialog, select the stored procedures you want to update, and then click the Update button.

##### Developing reports from stored procedures

When you have added some stored procedures to a catalog, you can then use them to develop reports as required.

1. Make sure the catalog where the stored procedure locates is opened, then click the New Report Set button on the Standard toolbar.
2. In the New Report set dialog, specify the title and layout of the first report in the report set as required, then click OK.
3. In the Data screen of the report wizard, choose the stored procedure with which you want to build the report, then follow the report wizard to create the report.

Note: When running a report which is built on a stored procedure, you will need to specify the values for the IN and INOUT parameters defined in the stored procedure. The values stored inside the stored procedure object in the catalog will be the default values. For details about specifying the values, see Editing parameter values in this document.

##### Creating cached query results for stored procedures

By default, when you run a report, JReport Engine fetches data from the database using the JDBC driver. For reports that are built on stored procedures which function as queries, JReport enables you to create cached result files and save them somewhere in your machine. Then, when you view these reports, you can choose to use the data from the cached query result file as opposed to the database.

For details about cached query results, see Cached query results.