OTL 4.0, Example 547 (Sybase SQL Server stored procedure call)

This example demonstrates a stored procedure call.

Source Code

#include <iostream>
using namespace std;

#include <stdio.h>
#define OTL_ODBC_UNIX // Compile OTL 4.0/ODBC
#include <otlv4.h>

otl_connect db; // connect object

void stored_proc(void)
// invoking stored procedure
{
otl_stream o(1, // buffer size should be equal to 1 in case of stored procedure call
"{call my_proc("
" :A<int,inout>, "
" :B<char[31],out>, "
" :C<char[31],in> "
")}",
// stored procedure call
db // connect object
);

o.set_commit(0); // set stream auto-commit off since
// the stream does not generate transaction

o<<1<<"Test String1"; // assigning :1 = 1, :3 = "Test String1"

int a;
char b[31];

o>>a>>b;
cout<<"A="<<a<<", B="<<b<<endl;

}

int main()
{
otl_connect::otl_initialize(); // initialize ODBC environment
try{
db.rlogon("scott/tigger@freetds_sybsql"); // connect to ODBC
// It's recommended to use auto_commit_off() when OTL is used
// with FreeTDS/ODBC against Sybase in order to set the DB
// session's auto-commit to OFF. otl_connect::rlogon()'s auto-commit
// parameter has not effect. auto_commit_off() is a workaround, until
// a fix becomes available.
db.auto_commit_off();
otl_cursor::direct_exec(db,"drop procedure my_proc",0);
otl_cursor::direct_exec
(db,
"CREATE PROCEDURE my_proc "
" @A int out, "
" @B varchar(60) out, "
" @C varchar(60) "
"AS "
"BEGIN "
" SELECT @A=@A+1"
" SELECT @B=@C "
"END"
); // create stored procedure

stored_proc(); // invoking stored procedure
}
catch(otl_exception& p){ // intercept OTL exceptions
cerr<<p.msg<<endl; // print out error message
cerr<<p.code<<endl; // print out error code
cerr<<p.var_info<<endl; // print out the variable that caused the error
cerr<<p.sqlstate<<endl; // print out SQLSTATE message
cerr<<p.stm_text<<endl; // print out SQL that caused the error
}
db.logoff(); // disconnect from the data source
return 0;
}


Output

 A=2, B=Test String1

Examples Contents Go Home

Copyright © 1996-2017, Sergei Kuchin, email: skuchin@gmail.com, skuchin@gmail .

Permission to use, copy, modify and redistribute this document for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.