User-defined stored procedures

With Embedded SQL version 11.1 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, output, or both input and output. For details on stored procedures, see the Transact-SQL User’s Guide.


Syntax

Valid stored procedure names consist of upper- and lowercase letters of the alphabet, “$”, “_”, and “#”.

Do not include the use statement in a stored procedure.

To execute a stored procedure, use the following syntax:

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

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 retcode is a status variable, a_proc is the stored procedure, par1 is an input parameter, and par2 is an output parameter:

exec sql begin declare section; 
 CS_INT  par1; 
 CS_INT  par2; 
 CS_SMALLINT  retcode; 
 exec sql end declare section; 
     ... 
 exec sql exec :retcode = a_proc :par1, :par2 out;

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;
 CS_CHAR  pub_id(4);
 CS_CHAR  name(45);
 CS_CHAR  city(25);
 CS_CHAR  state(2);
 CS_SMALLINT  retcode;
 exec sql end declare section;
    . . .
 exec sql exec :retcode = get_publishers :pub_id
                   into :name :city :state;

See Chapter 10, “Embedded SQL Statements: Reference Pages,” for a more detailed example of the exec statement.


Conventions

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