User-defined stored procedures

With Embedded SQL version 11.1 and later, you can execute stored procedures with select statements that return data rows. Stored procedures can return results to your program through output parameters and through a return status variable.

Stored procedure parameters can be either input or both input and output. For details on stored procedures, see the Transact-SQL User’s Guide.


Syntax

Valid stored procedure names consist of uppercase and lowercase letters and the characters $, _, and #.

Do not include the use statement in a stored procedure.

To execute a stored procedure, use the following syntax:

exec sql [at connection_name] 
  exec [:status_variable = status_value] procedure_name
 [([[@parameter_name =]parameter_value [out[put]]],...)]
  [into :hostvar_1 [:indicator_1]
  [, hostvar_n [indicator_n, ...]]]
  [with recompile] 
 end-exec

where:

NoteIn Embedded SQL, the exec keyword is required to execute a stored procedure. You cannot substitute execute for exec.


Stored procedure example

The following example shows a call to a stored procedure where RET-CODE is a status variable, a_proc is the stored procedure, PAR–1 is an input parameter, and PAR–2 is an output parameter:

exec sql begin declare section end-exec 
  01  PAR-1              PIC S9(9) COMP. 
  01  PAR-2              PIC S9(9) COMP. 
  01  RET-CODE           PIC S9(4) COMP. 
 exec sql end declare section end-exec 
     . . . 
     exec sql exec :RET-CODE=a_proc :PAR-1, 
                   :PAR-2 out end-exec. 

The next example demonstrates the use of a stored procedure that retrieves data rows. The name of the stored procedure is “get_publishers”:

exec sql begin declare section end-exec.
  01  PUB-ID            PIC X(4).
  01  NAME              PIC X(45).
  01  CITY              PIC X(25).
  01  STATE             PIC X(2).
  01  RET-CODE          PIC S9(9).
 exec sql end declare section end-exec.
    . . .
 exec sql exec :RET-CODE = get_publishers :PUB-ID
                   into :NAME :CITY :STATE END-EXEC.

See Chapter 10, “Open Client/Server Configuration File” for a more detailed example of the exec statement.


Conventions

The datatypes of the stored procedure parameters must be compatible with the COBOL host variables. Client-Library only converts certain combinations. See Chapter 4, “Using Variables” for a table of compatible datatypes.