Programming Interfaces Guide
CHAPTER 2. The Embedded SQL Interface
This section describes the use of SQL procedures in Embedded SQL.
Database procedures can be both created and called from Embedded SQL. A CREATE PROCEDURE statement can be embedded just like any other DDL statement. A CALL statement can also be embedded, or it can be prepared and executed. Here is a simple example of both creating and executing a stored procedure in Embedded SQL:
EXEC SQL CREATE PROCEDURE pettycash( IN amount DECIMAL(10,2) ) BEGIN UPDATE account SET balance = balance - amount WHERE name = 'bank'; UPDATE account SET balance = balance + amount WHERE name = 'pettycash expense'; END; EXEC SQL CALL pettycash( 10.72 );
If you wish to pass host variable values to a stored procedure, or retrieve the output variables, you prepare and execute a CALL statement. The example illustrates the use of host variables. Both the USING and INTO clauses are used on the EXECUTE statement.
EXEC SQL BEGIN DECLARE SECTION; double hv_expense; double hv_balance; EXEC SQL END DECLARE SECTION; EXEC SQL CREATE PROCEDURE pettycash( IN expense DECIMAL(10,2), OUT endbalance DECIMAL(10,2) ) BEGIN UPDATE account SET balance = balance - expense WHERE name = 'bank'; UPDATE account SET balance = balance + expense WHERE name = 'pettycash expense'; SET endbalance = ( SELECT balance FROM account WHERE name = 'bank' ); END; EXEC SQL PREPARE S1 FROM 'CALL pettycash( ?, ? )'; EXEC SQL EXECUTE S1 USING :hv_expense INTO :hv_balance;
Database procedures can also contain SELECT statements. The procedure is declared using a RESULT clause to specify the number, name, and types of the columns in the result set. Result set columns are different from output parameters. For procedures with result sets, the CALL statement can be used in place of a SELECT statement in the cursor declaration:
EXEC SQL BEGIN DECLARE SECTION; char hv_name[100]; EXEC SQL END DECLARE SECTION; EXEC SQL CREATE PROCEDURE female_employees() RESULT( name char(50) ) BEGIN SELECT emp_fname || emp_lname FROM employee WHERE sex = 'f'; END; EXEC SQL PREPARE S1 FROM 'CALL female_employees()'; EXEC SQL DECLARE C1 CURSOR FOR S1; EXEC SQL OPEN C1; for(;;) { EXEC SQL FETCH C1 INTO :hv_name; if( SQLCODE != SQLE_NOERROR ) break; printf( "%s\\n", hv_name ); } EXEC SQL CLOSE C1;
In this example, the procedure has been invoked with an OPEN statement rather than an EXECUTE statement. The OPEN statement causes the procedure to execute until it reaches a SELECT statement. At this point, C1 is a cursor for the SELECT statement within the database procedure. You can use all forms of the FETCH command (backward and forward scrolling) until you are finished with it. The CLOSE statement terminates execution of the procedure.
If there had been another statement following the SELECT in the procedure, it would not have been executed. In order to execute statements following a SELECT, use the RESUME cursor-name command. The RESUME command will either return the warning SQLE_PROCEDURE_COMPLETE, or it will return SQLE_NOERROR indicating that there is another cursor. The example illustrates a two-select procedure:
EXEC SQL CREATE PROCEDURE people() RESULT( name char(50) ) BEGIN SELECT emp_fname || emp_lname FROM employee; SELECT fname || lname FROM customer; END; EXEC SQL PREPARE S1 FROM 'CALL female_employees()'; EXEC SQL DECLARE C1 CURSOR FOR S1; EXEC SQL OPEN C1; while( SQLCODE == SQLE_NOERROR ) { for(;;) { EXEC SQL FETCH C1 INTO :hv_name; if( SQLCODE != SQLE_NOERROR ) break; printf( "%s\\n", hv_name ); } EXEC SQL RESUME C1; } EXEC SQL CLOSE C1;
These examples have used static cursors. Full dynamic cursors can also be used for the CALL statement
For a description of dynamic cursors, see Dynamic SELECT statement.
The DESCRIBE statement works fully for procedure calls. A DESCRIBE OUTPUT produces a SQLDA that has a description for each of the result set columns.
If the procedure does not have a result set, the SQLDA has a description for each INOUT or OUT parameter for the procedure. A DESCRIBE INPUT statement will produce a SQLDA having a description for each IN or INOUT parameter for the procedure.
DESCRIBE ALL describes IN, INOUT, OUT and RESULT set parameters. DESCRIBE ALL uses the indicator variables in the SQLDA to provide additional information.
The DT_PROCEDURE_IN and DT_PROCEDURE_OUT bits are set in the indicator variable when a CALL statement is described. DT_PROCEDURE_IN indicates an IN or INOUT parameter and DT_PROCEDURE_OUT indicates an INOUT or OUT parameter. Procedure RESULT columns have both bits clear.
After a describe OUTPUT, these bits can be used to distinguish between statements that have result sets (need to use OPEN, FETCH, RESUME, CLOSE) and statements that do not (need to use EXECUTE).
For a complete description, see DESCRIBE statement.
If you have a procedure that returns multiple result sets, you must redescribe after each RESUME statement if the result sets change shapes.
You need to describe the cursor, not the statement number, to describe the current position of the cursor.