/* 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 department records, and reads the records, * formatting and inserting them into the database until the end of * the RMS 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_depts.dat file record */ char d_code[5]; char d_name[31]; char d_mgr_id[6]; /* File definitions for reading the sql_depts.dat file */ FILE *depts_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 DEPTS"); /* Open the sequential file containing the department data records. */ #ifdef VMS depts_file = fopen("sql$sample:sql_depts.dat","r"); #endif #if defined(__osf__) || defined (_WIN32) depts_file = fopen("sql_depts.dat","r"); #endif /* This procedure uses the executable form for starting a transaction. */ EXEC SQL SET TRANSACTION READ WRITE RESERVING DEPARTMENTS, EMPLOYEES FOR EXCLUSIVE WRITE; /* Main loop until data file is empty */ while (get_line(depts_file) != NULL) { get_field(depts_file,d_code ,4); get_field(depts_file,NULL ,3); get_field(depts_file,d_name ,30); get_field(depts_file,NULL ,3); get_field(depts_file,d_mgr_id,5); /* INSERT a row in the DEPTS 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 DEPARTMENTS (DEPARTMENT_CODE, DEPARTMENT_NAME, MANAGER_ID) VALUES (:d_code, :d_name, :d_mgr_id); } /* Commit the transaction. */ EXEC SQL COMMIT; /* Close the sql_depts.dat data file. */ fclose(depts_file); /* Operator prompt message */ printf("\nProgram: DEPTS Loaded. Normal End-of-Job"); exit(1); /* Error handler for SQL errors */ HANDLE_ERROR: sql_signal(); exit(0); }