Previous Topic

Next Topic

Successful Use of the Copy Statement

When using the copy statement, you should avoid common problems and learn to use the statement correctly. Specifically, you should understand how to do the following:

Previous Topic

Next Topic

How You Check for Integrity Errors

When you use the copy statement, the data being copied is not checked for integrity errors.

To check the integrity of your data before using the copy statement, follow these steps:

  1. Use the create integrity statement (or the equivalent feature in VDBA) to impose the integrity constraint. For example:

    create integrity on personnel
        is name like '\[A-Z\]%' escape '\';

    If the search condition is not true for every row in the table, an error message is returned and the integrity constraint is rejected.

  2. If the integrity constraint is rejected, find the incorrect rows; for example:

    select name from personnel 
        where name not like '\[A-Z\]%' escape '\';

  3. Use Query-By-Forms to quickly scan the table and correct the errors.
  4. After ensuring that the data is correct, use the copy statement to load or unload your table.

As an additional check that the information was copied correctly, apply the integrity constraint after copying the table.

For more information on integrity checking and integrity constraints, see the chapter "Ensuring Data Integrity."

Previous Topic

Next Topic

Reloading Problems

When using the copy from statement, the following problems in the copy file are the most frequent causes for error messages:

Previous Topic

Next Topic

Invalid Data in the Copy File

If you try to load invalid data into a field, the row is rejected.

For example, the following record is rejected because February has only twenty-eight or twenty-nine days:

559-58-2543,31-feb-1998,Weir,100000.00,Executive

Previous Topic

Next Topic

Miscounted Fixed-Length Field Widths in the Copy File

If the widths of fixed-length fields are not correct, the copy statement can try to include data in a field that it cannot convert to the appropriate format.

For example, you receive an error message if you try to copy this row:

554-39-2699 01-oct-1998 Quinn 28000.00 Assistant

with the following copy statement:

copy table personnel (ssno = char(20),
    birthdate = char(11), 
    name = char(11),
    salary = char(9), 
    title = char(0)nl)
    from 'pers.data';

Because you specified char(20), or 20-character positions, for the ssno field, the copy statement includes part of the birth date in the value for the ssno field. When the copy statement tries to read the birth date, it reads "998 Quinn 2" which is not a valid date if birth date is defined as a date field; if defined as a char field, you get an "unexpected EOF" error.

Previous Topic

Next Topic

No nl Delimiter in the Copy File

When using fixed-length specifications in the copy statement, you must account for the "nl" (newline) character at the end of the record.

For example, you receive an error message if you try to copy these records:

554-39-2699 01-oct-1998 Quinn 28000.00 Programmer
335-12-1452 23-jun-1998 Smith 79000.00 Sr Analyst

with the following copy statement:

copy table personnel (ssno = char(12),
    birthdate = char(12), 
    name = char(6),
    salary = char(9), 
    title = char(10))
    from 'pers.data';

The format specified for the title field is char(10), which does not account for the newline character. The newline characters are converted to blanks, and the extra characters force the copy statement to begin reading a third record that ends abnormally with an unexpected end of file. Use char(10)nl to avoid this problem.

Previous Topic

Next Topic

Omitted Delimiters Between Fields in the Copy File

If you omit delimiters between fields in the data file, the record is rejected.

For example, the first record below has no delimiter between the employee's name and her salary:

123-45-6789,01-jan-1998,Garcia33000.00,Programmer246-80-1357,02-jan-1998,Smith,43000.00,Coder

If you try to copy these records with the following copy statement, you receive an error message because the copy statement attempts to read "Programmer" into the "salary" field:

copy table personnel
    (ssno = char(0), 
    birthdate = char(0),
    name = char(0), 
    salary = char(0),
    title = char(0)nl)
    from 'pers.data';

Previous Topic

Next Topic

Too Many Delimiters in the Copy File

Be careful not to include too many delimiters in the data file. This mistake frequently occurs when you use the comma as a delimiter and it also appears in the data.

For example, in the first row, the salary value contains a comma:

123-45-6789,01-jan-1998,Garcia,33,000.00,Programmer

246-80-1357,02-jan-1998,Smith,43000.00,Coder

If you try to copy these records with the following copy statement, you receive an error message:

copy table personnel 
    (ssno = char(0), 
    birthdate = char(0),
    name = char(0), 
    salary = char(0), 
    title = char(0))
    from 'pers.data';

You receive an error because the copy statement reads:

It attempts to read "246-80-1357" as the birthdate, which produces the error.

If you specified "title = char(0)nl", the copy statement still reads "33" as the salary, but it reads "000.00,Programmer" as the title. This is because it looks for a newline rather than a delimiter at the end of the title. It reads the next row correctly. Although an error message is not generated, the title field for one row is incorrect.

Previous Topic

Next Topic

Error Handling with the Copy Statement

When using the copy statement, use the various options on the with clause to control how invalid data is handled.

Previous Topic

Next Topic

Stop or Continue the Copy

Use the with on_error clause to stop or continue copying the data when an error occurs. In the following example, the copy continues after finding an error:

copy table personnel 
    (name= char(0), 
    dept = char(0)nl) 
    from 'pers.data'
    with on_error = continue;

The default is to terminate at the first error.

Previous Topic

Next Topic

Stop the Copy After a Specified Number of Errors

To stop the copy after a certain number of errors, specify an error count with the error_count=n clause. For example:

copy table personnel
    (name = char(0), 
    dept = char(0)nl) 
    from 'pers.data'
    with error_count = 10;

The default error_count is 1.

This clause is not meaningful when used with the on_error=continue clause. See the Error_ Count Option for the copy statement in the SQL Reference Guide.

Previous Topic

Next Topic

Roll Back Rows

By default, copying data stops after finding an error. If you do not want to back out the rows already copied, specify with rollback = disabled. For example:

copy table personnel 
    (name = char(0), 
    dept = char(0)nl) 
    from 'pers.data'
    with rollback = disabled;

Use the with rollback clause with the copy from statement only. Rows are never backed out of the copy file if copy into is terminated. For additional information, see the SQL Reference Guide.

Previous Topic

Next Topic

Log Errors During Copy

Use the with log clause to put invalid rows into a log file for future analysis. The following query is terminated after ten errors, and these errors are placed in a log file named badrows.data:

copy table personnel 
    (name = char(0), 
    dept = char(0)nl) 
    from 'pers.data'
    with error_count = 10,
    log = 'badrows.data';

Previous Topic

Next Topic

Continue the Copy and Log Errors

By using both log and on_error = continue in the clause, put invalid rows in a log file and continue to process valid ones. Correct the rows in the log file and load them into the database. For example:

copy table personnel 
    (name = char(0), 
    salary = char(0)nl)
    from 'pers.data'
    with on_error = continue,
    log = 'badrows.data';

Previous Topic

Next Topic

Troubleshooting Tips for Data Loading

Follow these tips if you have trouble loading your data into the designated tables:

If you are not able to load data from binary files:


© 2007 Ingres Corporation. All rights reserved.