#include #include #include /* ** Example of X/Open CAE SQL (1992) compliant embedded program. ** ** The program tries to connect to the DEFAULT database as SYSADM ** with password SYSADM. ** It will then declare a cursor for the standard view ** INFORMATION_SCHEMA.TABLES, and fetch all records from it. ** ** If any error occurs, the print_sqlerror routine will be called to ** print an error message. */ /* ** X/Open does not allow a the same variable to be defined in several ** DECLARE SECTIONs, so we declare SQLSTATE here so that all routines ** in the file can use the same variable. */ exec sql BEGIN DECLARE SECTION; static char SQLSTATE[6]; exec sql END DECLARE SECTION; void print_sqlerror() /* ** print_sqlerror prints an error message for the latest error. ** Programmed according to the X/Open CAE specification. */ { exec sql BEGIN DECLARE SECTION; int i; int exceptions; varchar message[255]; exec sql END DECLARE SECTION; exec sql GET DIAGNOSTICS :exceptions = NUMBER; /* How many exceptions? */ for (i=1; i<=exceptions; i++) { exec sql GET DIAGNOSTICS EXCEPTION :i :message = MESSAGE_TEXT; printf("%s\n", message); } } main() { exec sql BEGIN DECLARE SECTION; varchar schema[129]; varchar table [129]; char type [21]; exec sql END DECLARE SECTION; exec sql DECLARE MYCURSOR CURSOR FOR SELECT TABLE_SCHEMA,TABLE_NAME,TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES; exec sql WHENEVER SQLERROR goto error_exit; /* Connect to default database */ exec sql CONNECT TO '' USER 'SYSADM' USING 'SYSADM'; exec sql OPEN MYCURSOR; while (1) { exec sql FETCH MYCURSOR INTO :schema,:table,:type; if (strcmp(SQLSTATE, "02000") == 0) break; /* No more rows */ printf("%s %s %s\n", schema, table, type); } exec sql CLOSE MYCURSOR; exec sql COMMIT; exec sql DISCONNECT ALL; exit(0); error_exit: print_sqlerror(); exec sql WHENEVER SQLERROR CONTINUE; exec sql ROLLBACK; exec sql DISCONNECT ALL; exit(0); return 0; }