OTL 4.0, Example 577 (#define OTL_THROWS_ON_SQL_SUCCESS_WITH_INFO, ODBC, Sybase SQL Server)

This example demonstrates #define OTL_THROWS_ON_SQL_SUCCESS_WITH_INFO in a combination with #define OTL_EXTENDED_EXCEPTION (also see the otl_exception's extended fields).

Source Code

#include <iostream>
using namespace std;

#include <stdio.h>
#define OTL_ODBC_UNIX // Compile OTL 4.0/ODBC
#define OTL_EXTENDED_EXCEPTION // Enable extended field in otl_exception
#define OTL_THROWS_ON_SQL_SUCCESS_WITH_INFO
#include <otlv4.h> // include the OTL 4.0 header file

otl_connect db; // connect object

void insert()
// insert rows into table
{
try{
// Setting the "OTL throws on SQL_SUCCESS_WITH_INFO flag" to true
db.set_throw_on_sql_success_with_info(true);
// Executing a T-SQL batch of INSERT statements.
otl_cursor::direct_exec
(db,
"SET NOCOUNT ON; " // NOCOUNT needs to be set to ON in order to
// work with OTL.
"INSERT INTO test_tab VALUES(1,'Name1'); "
"INSERT INTO test_tab VALUES(2,'Name2'); "
"INSERT INTO test_tab VALUES(2,'*Name2'); "
"INSERT INTO test_tab VALUES(3,'Name3') ",
otl_exception::enabled
);
}catch(otl_exception& p){ // intercept OTL exceptions
cerr<<p.msg<<endl; // print out error message
cerr<<p.stm_text<<endl; // print out SQL that caused the error
cerr<<p.sqlstate<<endl; // print out SQLSTATE message
cerr<<p.var_info<<endl; // print out the variable that caused the error
if(p.arr_len>0){ // checking if the extended fields were populated
for(int j=0;j<p.arr_len;++j){
cout<<"MSG["<<j<<"]="<<p.msg_arr[j]<<endl; // message array
cout<<"SQLSTATE["<<j<<"]="<<p.sqlstate_arr[j]<<endl; // sqlstate array
cout<<"CODE["<<j<<"]="<<p.code_arr[j]<<endl; // code array
}
}
}
// Setting the "throw flag" back to false
db.set_throw_on_sql_success_with_info(false);

}

int main()
{
otl_connect::otl_initialize(); // initialize ODBC environment
try{

db.rlogon("scott/tigger@freetds_sybsql"); // connect to MS SQL Server
// 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 table test_tab",
otl_exception::disabled // disable OTL exceptions
); // drop table

otl_cursor::direct_exec
(
db,
"create table test_tab(f1 int not null unique, f2 varchar(30))"
); // create table

insert(); // insert records into table

}

catch(otl_exception& p){ // intercept OTL exceptions
cerr<<p.msg<<endl; // print out error message
cerr<<p.stm_text<<endl; // print out SQL that caused the error
cerr<<p.sqlstate<<endl; // print out SQLSTATE info.
cerr<<p.var_info<<endl; // print out the variable that caused the error
if(p.arr_len>0){ // checking if the extended fields were populated
for(int j=0;j<p.arr_len;++j){
cout<<"MSG["<<j<<"]="<<p.msg_arr[j]<<endl; // message array
cout<<"SQLSTATE["<<j<<"]="<<p.sqlstate_arr[j]<<endl; // sqlstate array
cout<<"CODE["<<j<<"]="<<p.code_arr[j]<<endl; // code array
}
}
}

db.logoff(); // disconnect from MS SQL Server
return 0;

}

Output

[FreeTDS][SQL Server]Incorrect syntax near ';'.

SET NOCOUNT ON; INSERT INTO test_tab VALUES(1,'Name1'); INSERT INTO test_tab VALUES(2,'Name2'); INSERT INTO test_tab VALUES(2,'*Name2'); INSERT INTO test_tab VALUES(3,'Name3')
42000

MSG[0]=[FreeTDS][SQL Server]Incorrect syntax near ';'.

SQLSTATE[0]=42000
CODE[0]=102
MSG[1]=[FreeTDS][SQL Server]Incorrect syntax near ';'.

SQLSTATE[1]=42000
CODE[1]=102
MSG[2]=[FreeTDS][SQL Server]Incorrect syntax near ';'.

SQLSTATE[2]=42000
CODE[2]=102
MSG[3]=[FreeTDS][SQL Server]Incorrect syntax near ';'.

SQLSTATE[3]=42000
CODE[3]=102



Examples Contents Go Home

Copyright © 1996-2024, Sergei Kuchin, email: skuchin@gmail.com, skuchin@gmail.com/a>.

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.