/* Copyright © 1995, 1997, Oracle Corporation. All Rights Reserved. */ /* ABSTRACT: * * This program demonstrates the use of the SQL precompiler for the C * language to load an Oracle Rdb database from a stream (flat) file. * * This program attaches to an existing Oracle Rdb database, opens a data * file containing degree records, and reads the records, formatting * and inserting them into the database until the end of the data * file is reached. Then the program commits the transaction. */ #include #include #include #include #ifdef VMS #include "sql$sample:sql_load_rtl.sc" #endif #if defined(__osf__) || defined (_WIN32) #include #endif main( ) { /* Fields to receive strings read from the sql_degrees.dat file record */ char deg_id[6]; char col_code[5]; char date_rec[5]; char degree[4]; char deg_field[16]; /* File definitions for reading the sql_degrees.dat file */ FILE *degrees_file; /* Declarations for error handling */ int return_status; /* Define the SQLCA. */ EXEC SQL INCLUDE SQLCA; /* Declare the database. */ EXEC SQL DECLARE ALIAS FILENAME personnel; /* Set up error handling for failures on execution of SQL statements. */ EXEC SQL WHENEVER SQLERROR GOTO HANDLE_ERROR; /* Operator message to the terminal */ printf("\nProgram: Loading DEGREES"); /* Open the sequential file containing the job history data records. */ #ifdef VMS degrees_file = fopen("sql$sample:sql_degrees.dat","r"); #endif #if defined(__osf__) || defined (_WIN32) degrees_file = fopen("sql_degrees.dat","r"); #endif /* This procedure uses the executable form for starting a transaction. */ EXEC SQL SET TRANSACTION READ WRITE RESERVING DEGREES FOR EXCLUSIVE WRITE; /* Main loop until data file is empty */ while (get_line(degrees_file) != NULL) { get_field(degrees_file,deg_id ,5); get_field(degrees_file,NULL ,3); get_field(degrees_file,col_code ,4); get_field(degrees_file,NULL ,3); get_field(degrees_file,date_rec ,4); get_field(degrees_file,NULL ,5); get_field(degrees_file,degree ,3); get_field(degrees_file,NULL ,3); get_field(degrees_file,deg_field,15); /* INSERT a row in the DEGREES table. * The list of names in the VALUES clause corresponds to the * host variables containing the values. The list of names that * follows the INSERT clause names the columns in the table * that are to be inserted. */ EXEC SQL INSERT INTO DEGREES (COLLEGE_CODE, EMPLOYEE_ID, YEAR_GIVEN, DEGREE, DEGREE_FIELD) VALUES (:col_code, :deg_id, :date_rec, :degree, :deg_field); } /* Commit the transaction. */ EXEC SQL COMMIT; /* Close the sql_degrees.dat data file. */ fclose(degrees_file); /* Operator prompt message */ printf("\nProgram: DEGREES Loaded. Normal End-of-Job"); exit(1); /* Error handler for SQL errors */ HANDLE_ERROR: sql_signal(); exit(0); }