# OTL 4.0, Example 429 (OTL/ODBC, accessing the system data dictionary via ODBC functions, Sybase SQL Server)

This example demonstrates how to access the database system data dictionary via ODBC functions, OTL/ODBC, Sybase SQL Server. In theory, any ODBC driver that implements the necessary ODBC functions can be used with OTL/ODBC.

## Source Code

    #include <iostream>
    using namespace std;

    #include <stdio.h>

    #define OTL_ODBC_UNIX // Compile OTL 4.0/ODBC
    #include <otlv4.h> // include the OTL 4.0 header file

    otl_connect db; // connect object

    void select()
    {
     otl_stream s;

     s.set_all_column_types(otl_all_num2str|otl_all_date2str);
        // map all numeric and date/time output columns to strings for simplicity.

     s.open(100, // buffer size
             "$SQLTables",
             // get a list of all tables in the current database
             db // connect object
            );

     otl_column_desc* desc;
     int desc_len;
     desc=s.describe_select(desc_len);
        // describe the structure of the output columns of the result set.

     int rpc=0;
     while(!s.eof()){
       ++rpc;
       char str[512];
       cout<<"ROW#"<<rpc<<" ";
       for(int col_num=0;col_num<desc_len;++col_num){
         s>>str;
         if(s.is_null())
           cout<<desc[col_num].name<<"=NULL ";
         else
           cout<<desc[col_num].name<<"="<<str<<" ";
       }
       cout<<endl;
     }

    }

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

      db.rlogon("scott/tigger@sybsql"); // connect to ODBC
      select(); // query the system data dictionary via ODBC functions.

     }

     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 ODBC

     return 0;

    }

## Output

    ROW#1 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=sysalternates TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#2 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=sysattributes TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#3 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=syscolumns TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#4 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=syscomments TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#5 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=sysconstraints TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#6 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=sysdepends TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#7 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=sysindexes TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#8 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=sysjars TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#9 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=syskeys TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#10 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=syslogs TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#11 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=sysobjects TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#12 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=syspartitionkeys TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#13 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=syspartitions TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#14 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=sysprocedures TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#15 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=sysprotects TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#16 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=sysqueryplans TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#17 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=sysreferences TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#18 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=sysroles TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#19 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=syssegments TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#20 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=sysslices TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#21 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=sysstatistics TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#22 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=systabstats TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#23 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=systhresholds TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#24 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=systypes TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#25 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=sysusermessages TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#26 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=sysusers TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#27 TABLE_CAT=sybsql TABLE_SCHEM=dbo TABLE_NAME=sysxtypes TABLE_TYPE=SYSTEM TABLE REMARKS=NULL
    ROW#28 TABLE_CAT=sybsql TABLE_SCHEM=scott TABLE_NAME=test_tab TABLE_TYPE=TABLE REMARKS=NULL
    ROW#29 TABLE_CAT=sybsql TABLE_SCHEM=scott TABLE_NAME=test_tab2 TABLE_TYPE=TABLE REMARKS=NULL

Examples Contents Go Home

Copyright © 1996, 2007, Sergei Kuchin, email: skuchin@ispwest.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.