An Open SQL Stored Procedure Example
This example illustrates how to execute a stored procedure using an RWDBOSql object. The stored procedure used here is assumed to take one input-output parameter of type int. It returns two result sets: the first consists of character strings; the second of two columns of types int and RWDecimalPortable. The example assumes an error handler is associated with the connection in use, and doesn't check for any errors after calls to the RWDBOSql object.
 
 
const size_t NUMBER_OF_ROWS = 10;
 
RWDBTBuffer<int> index; //1
index.paramType(RWDBColumn::inOutParameter); //2
index[0] = 100; //3
RWDBTBuffer<RWDBNativeType1> cursor1, cursor2; //4
RWDBTBuffer<RWCString> result1 (NUMBER_OF_ROWS); //5
RWDBTBuffer<int> result2_1 (NUMBER_OF_ROWS); //6
RWDBTBuffer<RWDecimalPortable> result2_2 (NUMBER_OF_ROWS); //7
 
RWDBOSql openSql ("BEGIN myProc (:index, :cursor1, :cursor2); END;",
RWDBOSql::Procedure); //8
 
openSql << index << cursor1 << cursor2; //9
 
openSql[0] >> result1; //10
openSql[1] >> result2_1 >> result2_2; //11
 
openSql.execute(conn); //12
 
long count = 0;
while ((count = openSql.fetch().rowsFetched()) > 0 ) { //13
for (int row = 0; row < count; row++) {
cout << "STRING VALUE :" << result1[row] << endl;
}
}
 
while ((count = openSql.fetch().rowsFetched()) > 0 ) { //14
for (int row = 0; row < count; row++) {
cout << "INT VALUE :" << result2_1[row] << endl;
cout << "DECIMAL VALUE: " << result2_2[row] << endl;
}
}
 
openSql.fetchReturnParams(); //15
cout << "OUT PARAM :" << index[0] << endl; //16
 
//1 Creates a buffer of size 1 and type int for the input-output parameter.
//2 Sets the parameter type of buffer to IN/OUT.
//3 Assigns a value to the buffer.
//4 Creates special buffers to hold the two Oracle PL/SQL cursor parameters. The Oracle PL/SQL cursor is the special datatype representing the result set. The DB Interface Module provides class RWDBNativeType1 to represent the Oracle PL/SQL cursor type.
//5 Creates a buffer of size NUMBER_OF_ROWS and type RWCString for the output values of the first result set. This stored procedure returns the first result set in the form of the reference cursor consisting of a string column.
//6 - //7 Creates buffers of size NUMBER_OF_ROWS and types int and RWDecimalPortable for the output values of the second result set. This stored procedure returns the second result set in the form of the reference cursor consisting of two columns of types int and RWDecimalPortable respectively.
//8 Associates an SQL statement with the RWDBOSql object. The second argument of the constructor sets the statement type. This information helps optimize binding and performance.
//9 Associates the input buffers with the RWDBOSql object. The second and third bound in parameters, cursor1 and cursor2, act as the result set's placeholders.
//10 Associates the output buffer for the first result (result1 in this case) with the openSQL object. This output buffer is bound to the first result set, indicated by index 0.
//11 Associates the output buffers for the second result (result2_1 and result2_2) with the openSQL object. These output buffers are bound to the second result set, indicated by index 1.
//12 Executes the SQL statement.
//13 Fetches the rows inside a while loop until all rows for the first result set are fetched. A successful fetch places the rows in the buffer bound to the openSQL object (result1 in this case). Note that the number of rows fetched is guaranteed to be less than or equal to the buffer's row size (NUMBER_OF_ROWS in this case). Completion of the fetch operation of all rows in the first result set is indicated when fetch() returns 0 rows.
//14 Uses another while loop to fetch all rows for the second result set, placing the rows in the buffers result2_1 and result2_2.
//15 Fetches the output values of the OUT and IN/OUT parameters by calling fetchReturnParams().
//16 Prints the fetched output value of the IN/OUT parameter using the bound buffer.