Stored procedures

In addition to the common relational database attributes (tables, fields, records, and so on) many SQL DBMS systems support stored procedures. A stored procedure is a compiled SQL program consisting of one or more SQL statements. A stored procedure can be used to define an SQL query that you can use over and over again. Furthermore, variables, conditional expressions, and variable arguments can be defined in the stored procedure so that you are prompted to provide information before the procedure is executed.



Since stored procedures can return a result set, they can provide a specific set of data when executed. In fact, Crystal Reports allows you to execute a stored procedure on an SQL database and use the returned data to design a report. If the stored procedure is designed to prompt a user for information to base its query on, Crystal Reports will prompt you for that information when you select the stored procedure for your report.

To select an SQL stored procedure

  1. Choose Options from the File menu.

    The Options dialog box appears.

  2. Click the Database tab and ensure that Stored Procedures is selected.

    Selecting "Stored Procedures" automatically displays any available stored procedures when you log on to an SQL database.

  3. In addition, you can specify Table name LIKE and Owner LIKE options if you wish.
    • "Table name LIKE" is based on the SQL LIKE clause. This option allows you to specify the kinds of table names you want to appear in the Choose SQL Table dialog box. You can use the underscore character (_) or the percent sign character (%) as wildcards with this function. The underscore character specifies any single character, while the percent sign signifies any character string. For example, DAV_ matches DAVE only, while DAV% matches DAVE and DAVID. Table name LIKE C% would display only those tables that have a table name beginning with the letter C.

    • "Owner LIKE" is also based on the SQL LIKE clause. The Owner LIKE option allows you to select the Owner (or Creator or Alias) of the table, not the table name itself. For example Owner LIKE C% would display only those tables that have an owner beginning with the letter C.

  4. Click OK to exit the Options dialog box.
  5. On the Start Page, click Blank Report.
  6. Locate and select the SQL Server data source that contains the stored procedure you want to use.
  7. Click Next to go to the Connection Information dialog box.
  8. Enter the required information to log in.
  9. Click Finish.

To change a parameter in a stored procedure

  1. Highlight an SQL stored procedure in the Stored Procedures folder, and click the > arrow to add it to the Selected Tables list.

    The Enter Parameter Values dialog box appears.

  2. Highlight a parameter in the Parameter Fields list.
  3. Assign a value by typing into the Discrete Value box and then click OK.

    You are returned to the Database Expert.

  4. Click OK and create your report using the fields in the stored procedure.

The first time you refresh your report's data the parameter you entered is used. Only the records that satisfy that value are used in your report. Subsequent refreshing prompts for new parameter values.