OTL 4.0, Example 728 (MERGE statement, MS SQL)

This example demonstrates simple INSERT/MERGE/SELECT with MS SQL.

Source Code

#include <iostream>
#include <stdio.h>
using namespace std;

#define OTL_ODBC_MSSQL_2008 // Compile OTL 4.0/MS SQL SNAC
#include <otlv4.h> // include the OTL 4.0 header file

otl_connect db; // connect object

void insert(void)
// insert rows into table

 otl_stream o(10, // stream buffer size in logical rows.
"insert into test_tab "
"values(:f1<int>,:f2<char[31]>)",
                 // INSERT statement
              db // connect object
             );
o<<1<<"Line1";
o<<2<<"Line21";
o.flush();
}

void merge(void)
// merge rows into table

 otl_stream o(10, // stream buffer size in logical rows.
"merge into test_tab as d "
"using (select cast(:f1<int> as int) f1, "
" cast(:f2<char[31]> as varchar(30)) f2) s "
"on (s.f1=d.f1) "
"when matched then update set f2=s.f2+'--'+convert(varchar,s.f1) "
"when not matched then insert (f1,f2) values(s.f1,s.f2);",
                 // MERGE statement
              db // connect object
             );
o<<2<<"Line22";
o<<3<<"Line3";
o.flush();
}

void select(void)

 otl_stream i(5, // stream buffer size in logical rows
              "select * from test_tab ",
                 // SELECT statement
              db // connect object
             ); 
// SELECT output columns
 int f1;
 char f2[31];

 while(!i.eof()){ // while not end-of-data
  i>>f1>>f2;
cout<<"f1="<<f1<<", f2="<<f2<<endl;
 }
 
}

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

  db.rlogon("scott/tiger@mssql2008"); // connect to the database

  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, f2 varchar(30))"
    );  // create table

db<<"create unique index ind001 on test_tab(f1)";
// create unique index

  insert(); // insert records into table
merge(); // merge records into table
  select(); // select records from 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 message
  cerr<<p.var_info<<endl; // print out the variable that caused the error
 }

 db.logoff(); // disconnect from the database

 return 0;

}

Output
f1=1, f2=Line1
f1=2, f2=Line22--2
f1=3, f2=Line3

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

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.