IBM Support

Copying tables between databases

Question & Answer


Question

How do you copy a table from one database to another? 

Answer

You can copy a table from one database to another database in either of the following ways:

  • Using nzsql to extract the data and nzload to load data
  • Using external tables to copy a database table
Each method is described below.

Using nzsql
1. Extract the table from the source database by running the following command:
    nzsql -db <database> -u <user> -pw <password> -A -t -c "select * from <table>" -o <file>
  • The -A option specifies that the data is not aligned by whitespace characters but rather aligned in table output mode
  • The -t option specifies that NPS outputs rows of data only; do not output column headers
  • The -c option specifies that NPS runs the SQL enclosed in quotes as a single query
  • The -o option specifies that NPS sends query results to a file

2. Load the data into the destination database by running the following command:
    nzload -db <database> -pw <password> -t <table> -df <datafile> -delim '|'

This command loads the data from the file into the second database. The table names do not need to be the same, however they must have the same structure. For more information about using nzload, refer to the NPS Administrator's Guide.

Using External Tables
1. For each database create an external table by running the following SQL:
    create external table table_external sameas

    using (dataobject ('/home/nz/outputfile.data')

    delimiter '|');

2. Unload the data from the first database by running the following command:
    nzsql -db <database> -u <username> -pw <password> -c "insert into table_external
    select * from <table>"

3. Reverse the process against the second database by running the following command:
    nzsql -db <database> -u <username> -pw <password> -c "insert into <table> example select *
    from example_extern"

Using Cross Database Syntax with a CTAS statement
1. From the destination database issue the following CTAS statement:
    CREATE TABLE <table> as select * from <source_database>..<source_table>;
[{"Product":{"code":"SSULQD","label":"IBM PureData System"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"Database","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"1.0.0","Edition":"","Line of Business":{"code":"LOB10","label":"Data and AI"}}]

Historical Number

NZ144813

Document Information

More support for:
IBM PureData System

Software version:
1.0.0

Document number:
466607

Modified date:
17 October 2019

UID

swg21574768