An Open SQL Stored Procedure Example
This example illustrates how a stored procedure can be executed using an
RWDBOSql object. The stored procedure used here is assumed to take one input-output parameter of type
int, and to return two result sets, the first consisting of character strings and the second of two columns of types
int and
RWDecimalPortable. This stored procedure has a return value of type
int.
This example assumes that an error handler is associated with the connection, and therefore doesn’t check for any errors after calls to the
RWDBOSql object.
#define NUMBER_OF_ROWS 10 // Indicates the number of rows
// for the buffer
#define MAXSTRING_SIZE 255 // Maximum length of a string
// in the buffer
RWDBOSql openSql;
int inputParam = 100;
RWDBNullIndicator indA = 0;
RWDBTBuffer<int> paramBuffer(&inputParam, &indA, 1); //1
paramBuffer.paramType(RWDBColumn::inOutParameter); //2
char aString[NUMBER_OF_ROWS][MAXSTRING_SIZE];
memset(aString, 0, NUMBER_OF_ROWS * MAXSTRING_SIZE );
RWDBNullIndicator nullInd[NUMBER_OF_ROWS];
RWDBTBuffer<char> outCharBuffer(aString[0], nullInd,
NUMBER_OF_ROWS, MAXSTRING_SIZE); //3
RWDBTBuffer<int> outIntBuff(NUMBER_OF_ROWS); //4
RWDBTBuffer<RWDecimalPortable> outDecBuff(NUMBER_OF_ROWS); //5
RWCString sybaseSyntaxSql("mySelectStoredProc"); //6
openSql.statement(sybaseSyntaxSql, RWDBOSql::Procedure); //7
openSql << paramBuffer; //8
openSql[0] >> outCharBuffer; //9
openSql[1] >> outIntBuff >> outDecBuff; //10
openSql.execute(conn); // Execute the stored procedure
long count = 0;
while ( (count = openSql.fetch().rowsFetched() ) > 0 ) { //11
for( int row = 0; row < count; row++ ){
cout << "STRING VALUE :" << &outCharBuffer[row] << endl;
}
}
while ( (count = openSql.fetch().rowsFetched() ) > 0 ) { //12
for( int row = 0; row < count; row++ ){
cout << "INT VALUE: " << outIntBuffer[row] << endl;
cout << "DECIMAL VALUE: " << outDecBuff[row] << endl;
}
}
openSql.fetchReturnParams(); //13
cout << "RETURN PARAM VALUE : "<< inputParam << endl;
RWDBValue returnValue = openSql.returnValue(); //14
cout << "RETURN VALUE :" << returnValue.asInt() << endl;