/* Copyright © 1995, 2002, Oracle Corporation. All Rights Reserved. */ /* SQL$INTRO_LOAD_EMPL_H.C * * This program demonstrates the use of DEC C and the SQL module language * to load an Oracle Rdb database from an OpenVMS RMS file. It calls the * SQL module SQL$INTRO_LOAD_EMPL_C.SQLMOD. * * This program attaches to the database created in Getting Started with * Oracle Rdb. It then opens the file SQL$INTRO_EMPL.DAT, which * contains employee records. The program reads and formats the * records and inserts them into the database until it reaches the end * of the data file. The program then commits the transaction. */ #include #include #include #include #include #include #include main( ) { /* Host variables for SQL calls. Character strings are one character longer * to hold the null value that terminates the string (for the C language * only). */ char employee_id[6], last_name[15], first_name[11], middle_initial[2], address_data[26], city[21], state[3], postal_code[6], ascii_birthdate[24], sex[2]; void set_trans(); void insert_data(); void commit_trans(); /* Structure to hold the BINTIM result value for the DATE variable. */ /* Note that the structure contains int L0 and int L1. */ struct { int l0, l1; } sql_date_vms, *date_vms = &sql_date_vms; /* Variable to hold blank spaces read from the data file. */ char filler1[3]; /* Variable to hold the line feed read from the data file. */ char filler2[1]; /* Text descriptor declaration. */ struct dsc$descriptor_s t_dsc; /* File definitions for reading the SQL$INTRO_EMPL.DAT file. */ FILE *employees_file; /* Return status and SQLCODE variables for error handling. */ int return_status; int sqlcode; /* Print message to the terminal */ printf("\nProgram: Loading EMPLOYEES"); /* Open the sequential file containing the employees data records. */ employees_file = fopen("SQL$SAMPLE:SQL$INTRO_EMPL.DAT","r"); /* Start the transaction by calling the SQL module language procedure * set_trans. */ set_trans(&sqlcode); /* If the call to the procedure returns a value other than 0, * use the sql_signal routine to display the error. */ if (sqlcode != 0) sql_signal(); /* Main loop until data file is empty. Read the employees_file. Store * the data in uniquely named variables, store the blank spaces in filler1, * and store the line feed in filler2. */ while ((return_status = fscanf(employees_file, "%5c%3c%14c%3c%10c%3c%1c%3c%25c%3c%20c%3c%2c%3c%5c%3c%23c%3c%1c%1c", employee_id, filler1, last_name, filler1, first_name, filler1, middle_initial, filler1, address_data, filler1, city, filler1, state, filler1, postal_code, filler1, ascii_birthdate,filler1, sex, filler2)) != EOF) { /* Add the null value string terminator to the input variables. * (This is a requirement for the C language only.) */ employee_id[5] = last_name[14] = first_name[10] = middle_initial[1] = address_data[25] = city[20] = state[2] = postal_code[5] = ascii_birthdate[23] = sex[1] = '\0'; /* Convert the birthdate to DATE data type format. */ t_dsc.dsc$b_class = DSC$K_CLASS_S; t_dsc.dsc$b_dtype = DSC$K_DTYPE_T; t_dsc.dsc$w_length = 23; t_dsc.dsc$a_pointer = ascii_birthdate; return_status = sys$bintim(&t_dsc, &sql_date_vms); if (return_status != SS$_NORMAL) { printf("\n\nInvalid date format in input file.'%s'", &ascii_birthdate); exit(return_status); } /* Insert a row in the EMPLOYEES table by calling the SQL module language * procedure insert_data and passing the values to the procedure. */ insert_data (&sqlcode, employee_id, last_name, first_name, middle_initial, address_data, city, state, postal_code, date_vms, sex); /* If the call to the procedure returns a value other than 0, * use sql_signal to display the error. */ if (sqlcode != 0) sql_signal(); /* End while loop. */ } /* Commit the transaction by calling the SQL module language procedure * commit_trans. */ commit_trans(&sqlcode); /* If the call to the procedure returns a value other than 0, * use sql_signal to display the error. */ if (sqlcode != 0) sql_signal(); else /* Display a message. */ printf("\nProgram: EMPLOYEES Loaded. Normal End-of-Job"); /* Close the data file. */ fclose(employees_file); }