Sales Order Import
You can view the documentation for the order import application by clicking on the links below:
Order Import Technical Specification    
Order Import User Guide    
The requirements for our sales order data import process are as follows:
- The sales order data is received in CSV format text files.
- The files are named with the prefix āorderā and extension ācsvā.
- Each file must have a unique name.
- The process must be automated to run every hour, and import all new order CSV files received.
- The data must be validated, and if there are any errors, the entire CSV file must be rejected, moved to an āErrorā directory, and all errors reported.
- A manual procedure is required to fix rejected files.
- If the data passes validation, load the data into the order tables.
- Move successfully imported files to a āProcessedā archive directory.
- Delete old error messages relating to the orders that have been successfully imported.
Fixing rejected files
- Manual intervention is required.
- An import error report will show the following information for each rejected file:
- Filename.
- A key value that uniquely identifies the order: Order Reference.
- CSV data record (all fields).
- An error message identifying the invalid data.
- Date and time error reported.
- Locate the rejected CSV file in the Error directory, via the filename on the error report.
- Edit the CSV file, and manually correct each error reported.
- Order Reference may be altered, but you must check that there are no existing orders with the same reference.
- Dates must be in the format DD/MM/YYYY.
- Ship Date must be on or later than the Order Date, in the format DD/MM/YYYY.
- Customer ID invalid: contact the sales department to either obtain the correct code, or have a new account created.
- Product ID invalid: contact the sales department for the correct code.
- Move the corrected file to the āReceivedā directory to be re-processed.
Example CSV File
The order data is received in CSV files, similar to the following example:
"Ord Ref","Order Date","Comm","Customer","Ship Date","Product","Qty"
TEST0001,27/06/2022,C,103,02/07/2022,101863,12
TEST0001,27/06/2022,C,103,02/07/2022,100890,24
TEST0001,27/06/2022,C,103,02/07/2022,102130,36
TEST0002,29/06/2022,C,101,02/07/2022,101863,12
TEST0002,29/06/2022,C,101,02/07/2022,100890,24
TEST0003,03/07/2022,D,101,05/07/2022,101863,12
TEST0003,03/07/2022,D,101,05/07/2022,100890,24
The first row of the CSV file is the header, containing a list of field names. The body of the file contains the order data, with a row for each order line. Each CSV file can contain one or more orders, each order having one or more lines.
| Field No | Name | Data Type | Size | Description |
|---|---|---|---|---|
| 1 | Ord Ref | Char | 10 | Order Reference. Maximum length 10 characters. Must be unique per order. |
| 2 | Order Date | Date | 10 | Date format DD/MM/YYYY. |
| 3 | Commission | Char | 1 | A to Z, may be NULL. |
| 4 | Customer ID | Number | 6 | Must exist on the Customer table. |
| 5 | Ship Date | Date | 10 | Date format DD/MM/YYYY. Must be on or later than the Order Date. |
| 6 | Product ID | Number | 6 | Must exist on the Product table. |
| 7 | Qty | Number | 8 | Quantity of product ordered. Must be a valid whole number > 0, and <= 99999999. |
Directories
The following Directory objects have been created in the database, to allow the applications to access the CSV files, process, and move them.
| Directory Name | Path |
|---|---|
| DATA_IN | D:\demo\oracle-appsdemo-main\data\DATA_IN |
| DATA_IN_ERROR | D:\demo\oracle-appsdemo-main\data\DATA_IN\error |
| DATA_IN_PROCESSED | D:\demo\oracle-appsdemo-main\data\DATA_IN\processed |
| DATA_OUT | D:\demo\oracle-appsdemo-main\data\DATA_OUT |
Modules
The application consists of the following program modules:
| Module Name | Description |
|---|---|
| IMPORT_ORDER.BAT | A DOS script that finds order CSV files, moves each file in turn to the DATA_IN directory, and runs the SQL*Plus script IMPORT_ORDER.SQL to process each file. |
| IMPORT_ORDER.SQL | SQL script that calls a PL/SQL package function to validate, and import the CSV data. |
| IMPORT.ORD_IMP | PL/SQL package function. Validates the CSV data. Records errors in the IMPORTERROR table. If there are no errors, the data is imported into the order tables of the database, and the CSV file is moved to DATA_IN_PROCESSED. If errors are found the file is moved to DATA_IN_ERROR. |
IMPORT_ORDER.BAT
GitHub Repository - import_order.bat
This is a Windows Batch Script that does the following:
Search the DATA_HOME\RECEIVED directory for CSV files containing order data.
For each CSV file found with the name āorder*.csvā:
- Copy the CSV file to the DATA_IN import directory.
- Run PL/SQL: execute script IMPORT_ORDER.SQL passing filename.
- Delete the CSV file from the received directory.
REM Set the application environment variables
CALL ..\config\SET_ENV
FOR /R %DATA_HOME%\RECEIVED %%F IN (ORDER*.CSV) DO (
ECHO CSV FILE FOUND: %%F
REM Copy the csv to the data import directory
COPY "%%F" "%DATA_HOME%\DATA_IN\%%~NXF"
REM Execute the sqlplus script to load the data
SQLPLUS %CONNECT_USER%/%CONNECT_PWD%@%DBCONNECT% @%APP_HOME%\SQL\IMPORT_ORDER.SQL "%%~NXF"
REM Tidy up - delete the csv file from the received directory
DEL "%%F"
)
IMPORT_ORDER.SQL
GitHub Repository - import_order.sql
This SQL script calls the PL/SQL package function IMPORT.ORD_IMP, passing the CSV filename.
SET SERVEROUTPUT ON
DECLARE
v_filename VARCHAR2(100) := '&1';
v_result BOOLEAN;
BEGIN
util_admin.log_message('Order Data Import from file: '||v_filename);
v_result := import.ord_imp(v_filename);
IF v_result THEN
util_admin.log_message('Success!');
COMMIT; /* No errors, so explicitly commit new order data to database */
ELSE
raise_application_error (-20099,'Order import failed. View errors in IMPORTERROR for file '||v_filename);
END IF;
EXCEPTION
WHEN OTHERS THEN
util_admin.log_message('Error importing file ' || v_filename,SQLERRM,'IMPORT_ORDER.SQL','B','E');
END;
/
EXIT
IMPORT.ORD_IMP

PL/SQL - import package spec
PL/SQL - import package body
The ord_imp PL/SQL function, in the import package, loads the CSV data into a staging table, validates the data, and loads valid data into the order tables. If validation fails, the errors are recorded, the CSV file is rejected, and no data is loaded into the order tables.
There are two functions that are specific to the order import process: ord_imp, and ord_valid.
The following generic functions are used by the import process. These functions can be re-used when creating additional data import processes.
| Package | Function/Procedure | Description |
|---|---|---|
| import | delete_error | Delete old error messages for orders that have been successfully imported. |
| import | import_error | Record validation error message on IMPORTERROR table. |
| util_file | load_csv | Load CSV file data into the IMPORTCSV table. |
| util_file | delete_csv | Delete rows from IMPORTCSV for each CSV file that has been processed. |
| util_file | rename_file | Rename the CSV file by moving it to directory DATA_IN_PROCESSED if order imported, or DATA_IN_ERROR if it failed validation. |
| util_string | get_field | Extract Nth field from a delimited string (the CSV record). |
| util_admin | log_message | Record errors in the application log table APPLOG. |
| orderrp | currentprice | Finds the current price for the specified product. |
The PL/SQL program ord_imp does not issue a COMMIT; the caller (or client) is responsible for committing. If SQL*Plus is configured with EXITCOMMIT ON, exiting the client will commit outstanding work.
The ord_imp function processes the CSV data as follows:
- Load CSV data into staging table IMPORTCSV:
- Call the package function UTIL_FILE.LOAD_CSV to load order data from a CSV file into the IMPORTCSV staging table.
- The load_csv function returns an integer FILEID, which identifies the group of records loaded from the CSV file into the staging table.
- If the file is not found:
- Log the error in APPLOG, and IMPORTERROR tables.
- Return to calling application with status FALSE (failed).
- Validate the data in the staging table for the current FILEID:
- A Boolean variable āvalidā is initially set to TRUE.
- All rows in the staging table are validated.
- Set column KEY_VALUE in table IMPORTCSV to a unique value, that identifies each order, in this case it will be the first field in the CSV file, ORDREF.
- For each field that fails validation:
- Insert an error message into the IMPORTERROR table (include the KEY_VALUE column).
- Valid is set to FALSE.
- Validation continues until all rows in the staging table have been checked, and all errors logged.
- If validation has failed (valid is FALSE):
- Insert an error message into the APPLOG table.
- Delete rows from the staging table IMPORTCSV for the FILEID currently being processed. Note that no data has been committed to the database. A rollback cannot be used to remove the data from IMPORTCSV as that would also remove all the error messages in IMPORTERROR.
- No data is inserted into the ORD and ITEM tables.
- Move the CSV file to the directory data_in/error.
- Return to the calling application with status FALSE (failed).
- Else if validation succeeded (valid is TRUE):
- Insert data from the staging table into the ORD and ITEM tables.
- Delete the rows in the staging table IMPORTCSV for the FILEID being processed.
- Delete error messages for previous failed import attempts from IMPORTERROR. Match with the KEY_VALUE column of IMPORTCSV.
- Move the CSV file to the directory data_in/processed.
- Return to the calling application with status TRUE (succeeded).
- If an unexpected error occurs:
- Raise an application error.
- Rollback transactions (ORD, ITEM, IMPORTCSV) to the initial savepoint prior to data being loaded into the staging table.
- There will be no error messages in IMPORTERROR pending commit, as validation passed.
- Log an error by inserting rows into APPLOG and IMPORTERROR tables.
- Move the CSV file to directory data_in/error.
- Return to calling application with status FALSE (failed).
Control returns to the calling SQL*Plus application.
- A message is displayed indicating whether import failed or succeeded, depending on the status returned by PL/SQL function ord_imp.
- COMMIT is explicitly issued:
- If validation failed, the only data pending commit are error messages in the IMPORTERROR and APPLOG tables.
- If validation passed, either everything worked and the commit is applied to the new rows inserted into ORD an ITEM, or there was an unexpected error.
- In the case of an unexpected error following successful validation, there will be no validation errors pending commit. The PL/SQL function issued a rollback to the state when the program started, prior to data being loaded into the staging table. Any pending changes to the ORD and ITEM tables in the current transaction were discarded by the rollback. There will be error messages awaiting commit in APPLOG and IMPORTERROR.
- Staging table IMPORTCSV rows marked for deletion are deleted.


