Collection Contents Index Import and export overview Importing data into a database pdf/chap11.pdf

User's Guide
   PART 1. Working with Databases
     CHAPTER 11. Importing and Exporting Data       

Exporting data from a database


You can export data from your database using one of the following methods:

This section describes each of these methods, and also describes some tips for dealing with NULL output.

Top of page  Unloading data using the UNLOAD TABLE statement

The UNLOAD TABLE statement is used for efficient export of data from a database table to a text file. You must have SELECT permission on the table to use the UNLOAD TABLE statement.

Example 1 

The following statement unloads the department table from the sample database into the file dept.txt in the server's current working directory. If you are running against a network server, the command unloads the data into a file on the server machine, not the client machine.

UNLOAD TABLE department
TO 'dept.txt'

The dept.txt file has the following contents:

100,'R & D',501
200,'Sales',902
300,'Finance',1293
400,'Marketing',1576
500,'Shipping',703

Notes 

For Info     For more information on the syntax, see UNLOAD TABLE statement.

Example 2 

The following example uses explicit settings for the DELIMITED BY and QUOTES clauses:

UNLOAD TABLE department
TO 'dept.txt'
DELIMITED BY '$'
QUOTES OFF

The resulting dept.txt file has the following contents:

100$R & D$501
200$Sales$902
300$Finance$1293
400$Marketing$1576
500$Shipping$703

If a delimiter character appears within a value and the QUOTES option is turned off, the character is replaced by its hexadecimal value preceded by \x. For example:

UNLOAD TABLE department
TO 'dept.txt'
DELIMITED BY '&'
QUOTES OFF

yields the following output file.

100&R \x26 D&501
200&Sales&902
300&Finance&1293
400&Marketing&1576
500&Shipping&703

Top of page  Exporting query results using Interactive SQL

You can export queries to a file from Interactive SQL either by using the OUTPUT statement or by redirecting output.

Top of page  Using the output statement

Data can be exported from a database to a variety of file formats using the Interactive SQL OUTPUT statement. This statement exports the results of the current query (the one displayed in the Interactive SQL Data window) and puts the results into a specified file. The output format can be specified on the output command.

For example, the following commands extract the employee table to a dBaseIII format file:

SELECT *
FROM employee;
OUTPUT TO employee.dbf
FORMAT dbaseiii;

Top of page  Using output redirection

Output redirection can be used to export data as an alternative to the OUTPUT statement.

The output of any command can be redirected to a file or device by putting the ># redirection symbol anywhere on the command. The redirection symbol must be followed by a file name, as follows:

SELECT *
FROM employee
># filename

Do not enclose the file name in quotation marks.

Output redirection is most useful on the SELECT statement. The SET OUTPUT_FORMAT command can be used to control the format of the output file.

Redirecting with error messages 

The >& redirection symbol redirects all output including error messages and statistics for the command on which it appears. For example:

SELECT *
FROM employee 
>& filename

outputs the SELECT statement to the file, followed by the output from the SELECT statement and some statistics pertaining to the command.

Do not enclose the file name in quotation marks.

The >& redirection is useful for getting a log of what happens during a READ command. The statistics and errors of each command are written following the command in the redirected output file.

If two > characters are used in a redirection symbol instead of one (>># or >>&.), the output is appended to the specified file instead of replacing the contents of the file. For output from the SELECT statement, headings are output if and only if the output starts at the beginning of the specified file and the output format supports headings.

Top of page  NULL value output

The most common reason to extract data is for use in other software products. The other software package may not understand NULL values.

There is a Interactive SQL option (NULLS) that allows you to choose how NULL values are output. Alternatively, you can use the IFNULL function to output a specific value whenever there is a NULL value.

For Info     For information on setting Interactive SQL options, see SET OPTION statement.

Top of page  Unloading a database using DBUNLOAD

The dbunload utility is used to unload an entire database in ASCII comma-delimited format and to create the necessary Interactive SQL command files to completely recreate your database. This may be useful for creating extractions, creating a backup of your database, or building new copies of your database with the same or a slightly modified structure.

If you want to rearrange your tables in the database, you can use dbunload to create the necessary command files and modify them as needed.

For Windows 3.x, the DBUNLOAD executable is named dbunloaw.exe.

For Info     For a full description of dbunload utility command-line switches, see the section The Unload utility.

Exporting a list of tables 

The dbunload utility can also export a list of tables, rather than the entire database. This is useful for retrieving data from a corrupted database that cannot be entirely unloaded.

The following statement unloads the data from the sample database (assumed to be running on the default database server with the default database name) into a set of files in the c:\temp directory. A command file to rebuild the database from the data files is created with the default name reload.sql in the current directory.

dbunload -c "dbn=asademo;uid=dba;pwd=sql" c:\temp
Top of page  

Collection Contents Index Import and export overview Importing data into a database pdf/chap11.pdf