Complex Stored Procedure Example
As an example of a stored procedure that does multiple
SELECT statements, let’s consider the Sybase system stored procedure
sp_help, which displays information about database objects. This example also demonstrates how to use class
RWDBValue to handle data without knowing its type.
To use this code, the <ver> placeholder in any library names would need to be replaced with the actual digits representing the library version number.
Example 11 – Unknown results from stored procedure
// Example Program 5 - Unknown results from stored procedure
#include <rw/rstream.h>
#include <rw/db/db.h>
void display_table(const RWDBTable& table) {
RWDBReader rdr = table.reader();
size_t numCols = table.numberOfColumns();
RWDBValue v;
while (rdr()) {
for (size_t i = 0; i < numCols; i++) {
rdr >> v;
std::cout << v.asString() << "\t";
}
std::cout << std::endl;
}
}
int
main() {
RWDBDatabase myDbase = RWDBManager::database(
"libctl<ver>12d.so", // Access Module name
"SYBASE100", // server name
"user", // user name
"pwd", // password
"DEMO_DB" // database name
);
RWDBConnection session1 = myDbase.connection();
RWCString procName("sp_help");
RWDBStoredProc sp_help = myDbase.storedProc(procName, session1);
RWDBResult result = sp_help.execute(session1);
RWDBTable table = result.table();
int i = 0;
while (table.isValid()) {
std::cout << std::endl << "TABLE " << ++i << std::endl;
display_table(table);
table = result.table();
}
return 0;
}
In this example, the result of the execute() method is examined, rather than discarded. Each table in the result is passed to the display_table() routine, which simply converts tabular data to strings and prints them out. On our system, four tables of results are processed by this program.