Unloading data from Db2 on Linux, UNIX, and Windows systems to a remote client

You can unload data from a database table on server on Db2 on Linux, UNIX, and Windows systems to a remote client.

The unload operation is used to stream data from a Db2 database to a remote client. The unload operation does not remove rows from the database, but rather stores the unloaded data in a flat file that is suitable for loading back into a Db2 database. You can use the unload operation also to stream data into a named pipe in a remote client.

Unloading data by using a named external table

To unload data from your database into a file by using an INSERT statement, execute the following query.

INSERT INTO EXT_EMPLOYEE SELECT * FROM EMPLOYEE;

Unloading data by using a transient external table

The data can be unloaded using a transient external table by issuing following the command.

CREATE EXTERNAL TABLE '/tmp/employee.dat' USING (DELIMITER ',’ REMOTESOURCE 'JDBC’) AS SELECT * FROM EMPLOYEE;

Sometimes the IBM® Data Server Driver for JDBC and SQLJ cannot write the external table file in the path that is specified in the DataObject options. For example, if you have a named external table, or the path that is specified in transient external table, the driver writes the external table file to the driver's default output directory.

Fetching Unload file name

As a part of IBM Data Server Driver for JDBC and SQLJ and the DB2Statement interface, the class ExternalTableResult exposes the unload file name, and the error information that uses the APIs described in table 1:
Table 1. ExternalTableResult Methods
ExternalTableResult Methods Purpose
Throwable getError () Returns the first error that occurred while writing the unload file.
String getUnloadFileName() Returns the name of the file where data has been unloaded.
Applications can invoke these public methods on the DB2Statement Interface as shown in the following example:
ExternalTableResult lr = ((DB2Statement) stmt).getExternalTableResult ();
   String unloadFileName = lr.getUnloadFileName ();
   Throwable error = lr.getError();