The WbCopy
is essentially the command line version of the
the DataPumper. For a more detailed explanation
of the copy process, please refer to that section. It basically chains a
WbExport
and a WbImport
statement without the need of an intermediate data file. The WbCopy
command requires
that a connection to the source and target database can be made at the same time from the computer
running SQL Workbench/J
![]() | |
Some JDBC drivers (e.g. PostgreSQL, jTDS and the Microsoft Driver) read the full result obtained from the database into memory. In that case, copying large results might require a lot of memory. Please refer to the chapter Common problems for details on how to configure the individual drivers if this happens to you. |
WbCopy
command.Parameter | Description |
---|---|
-sourceProfile |
The name of the connection profile to use as the source connection. If -sourceprofile is not specified, the current connection is used as the source. If the profile name contains spaces or dashes, it has to be quoted. This parameter supports auto-completion |
-sourceGroup |
If the name of your source profile is not unique across all profiles, you will need to specify the group in which the profile is located with this parameter. If the group name contains spaces or dashes, it has to be quoted. |
-sourceConnection |
Allows to specify a full connection definition as a single parameter (and thus does not require a pre-defined connection profile). The connection is specified with a comma separated list of key value pairs:
e.g.: For a sample connection string please see the documentation for WbConnect.
If this parmeter is specified, |
-targetProfile |
The name of the connection profile to use as the target connection. If
If the profile name contains spaces or dashes, it has to be quoted. This parameter supports auto-completion |
-targetGroup |
If the name of your target profile is not unique across all profiles, you will need to specify the group in which the profile is located with this parameter. If the group name contains spaces or dashes, it has to be quoted. |
-targetConnection |
Allows to specify a full connection definition as a single parameter (and thus does not require a pre-defined connection profile). The connection is specified with a comma separated list of key value pairs:
e.g.:
If this parmeter is specified, |
-commitEvery |
The number of rows after which a commit is sent to the target database. This parameter
is ignored if JDBC batching (-batchSize ) is used.
|
-deleteTarget |
Possible values:
If this parameter is set to true, all rows are deleted from the
target table using a |
-truncateTable |
Possible values:
If this parameter is set to true, all rows are remove from the
target table using a
Not all DBMS support the |
-mode |
Defines how the data should be sent to the database. Possible
values are |
-syncDelete |
If this option is enabled
Combined with an
If more than one table is copied, the delete process is started after
all inserts and updates have been processed. It is recommended to use the
To only generate the SQL statements that would synchronize two databases, you can use the command WbDataDiff |
-keyColumns |
Defines the key columns for the target table. This parameter
is only necessary if import is running in
It is assumed that the values for the key columns will never be |
-ignoreIdentityColumns |
Possible values: Controls if identity or auto-increment columns will be included in the import.
If this is used, the JDBC driver (of the target database) must correctly report the column to be excluded as
an AUTOINCREMENT column. This can be verified in the table definition display of the DbExplorer.
If the column is reported with |
-batchSize |
Enable the use of the JDBC batch update feature, by setting the size of the batch queue. Any value greater than 1 will enable batch modee. If the JDBC driver supports this, the INSERT (or UPDATE) performance can be increased.
This parameter will be ignored if the driver does not support batch updates or if
the mode is not UPDATE or INSERT (i.e. if |
-commitBatch |
Valid values: When using the |
-continueOnError |
Defines the behaviour if an error occurs in one of the statements.
If this is set to
With PostgreSQL |
-useSavepoint |
Possible values:
Controls if SQL Workbench/J guards every insert or update statement
with a savepoint to recover from individual error during import,
when Using a savepoint for each DML statement can drastically reduce the performance of the import. |
-trimCharData |
Possible values:
If this parameter is set to true, values from |
-showProgress |
Valid values:
Control the update frequence in the status bar (when running in
GUI mode). The default is every 10th row is reported. To disable
the display of the progress specify a value of 0 (zero) or the
value |
Parameter | Description | |||
---|---|---|---|---|
-sourceSchema |
The name of the schema to be copied. When using this parameter, all tables
from the specified schema are copied to the target. You must specify either
| |||
-sourceTable |
The name of the table(s) to be copied. You can either specifiy a
list of tables: | |||
-excludeTables |
The tables listed in this parameter will not be copied. This can be used when all but a few tables
should be copied from one database to another. First all tables specified through
This parameter supports auto-completion. | |||
-checkDependencies |
When copying more than one file into tables with foreign key constraints,
this switch can be used to import the files in the correct order (child tables first).
When | |||
-targetSchema | The name of the target schema into which the tables should be copied. When this parameter is not specified, the default schema of the target connection is used. | |||
-sourceWhere |
A WHERE condition that is applied to the source table.
| |||
-targetTable | The name of the table into which the data should be written. This parameter is ignored if more than one table is copied. | |||
-createTarget |
If this parameter is set to
When using this option with different source and target DBMS, the information about the data types to be used in the target database are retrieved from the JDBC driver. In some cases this information might not be accurate or complete. You can enhance the information from the driver by configuring your own mappings in workbench.settings. Please see the section Customizing data type mapping for details.
If the automatic mapping generates an invalid | |||
-removeDefaults |
Valid values are
This parameter is only valid in combination with | |||
-tableType |
When
When using the auto-completion for this parameter, all defined "create types" that
are configured in workbench.settings (or are part of the default settings) are displayed
together with the name of the DBMS they are used for. The list is not
limited to definitions for the target database! The specified type must nonetheless match a type
defined for the target connection. If you specify a type that does not exist, the default
For details on how to configure a CREATE TABLE template for this parameter, please refer to the chapter Settings related to SQL statement generation | |||
-skipTargetCheck |
Normally WbCopy will check if the specified target table does exist. However, some JDBC drivers
do not always return all table information correctly (e.g. temporary tables). If you know that the
target table exists, the parameter | |||
-dropTarget |
Possible values:
If this parameter is set to
For database systems that support it (Oracle, PostgreSQL), a | |||
-columns |
Defines the columns to be copied. If this parameter is not specified, then all matching columns are copied from source to target. Matching is done on name and data type. You can either specify a list of columns or a column mapping.
When supplying a list of columns, the data from
each column in the source table will be copied into the corresponding column (i.e.
one with the same name) in the target table.
If
A column mapping defines which column from the source table maps to which column
of the target table (if the column names do not match)
If This parameter is ignored if more than one table is copied. When using a SQL query as the data source a mapping cannot be specified. | |||
-adjustSequences |
Possible values: For DBMS that support sequences which are associated with a column, this parameter can be used to adjust the next value for the sequence to the maximum value of the imported data. This can also be used to synchronize identity columns for DBMS that allow overriding the generated values. Currently this is implemented for PostgreSQL, DB2 (LUW), H2 Database and HyperSQL (aka HSQLDB). | |||
-preTableStatement -postTableStatement |
This parameter defines a SQL statement that should be executed before the import
process starts inserting data into the target table. The name of the current
table (when e.g. importing a whole directory) can be referenced using
To define a statement that should be executed after all rows have been
inserted and have been committed, you can use the These parameters can e.g. be used to enable identity insert for MS SQL Server: -preTableStatement="set identity_insert ${table.name} on" -postTableStatement="set identity_insert ${table.name} off"
Errors resulting from executing these statements will be ignored. If you want
to abort the import in that case you can specify These statements are only used if more than one table is processed. | |||
-runTableStatementOnError |
Possible values:
Controls the execution of the post-table statement in case an error occurred while importing the data.
By default the post-table statement is executed even if the import was not successful. If this is
should not happen, use | |||
-ignorePrePostErrors |
Possible values:
Controls handling of errors for the SQL statements defined through the |
Parameter | Description |
---|---|
-sourceQuery |
The SQL query to be used as the source data (instead of a table).
This parameter is ignored if |
-columns |
The list of columns from the target table, in the order in which they appear in the source query. If the column names in the query match the column names in the target table, this parameter is not necessary. If you do specify this parameter, note that this is not a column mapping. It only lists the columns in the correct order . |
The WbCopy
command understands the same update mode
parameter as the WbImport
command. For a discussion on
the different update modes, please refer to the WbImport
command.
Using -mode=update,insert
ensures that all rows that are present in
the source table do exist in the target table and that all values for non-key columns
are identical.
When you need to keep two tables completely in sync, rows that are present in the
target table that do not exist in the source table need to be deleted. This is what the
parameter -syncDelete
is for. If this is enabled (-syncDelete=true
)
then SQL Workbench/J will check every row from the target table if it is present in the
source table. This check is based on the primary keys of the target table and
assumes that the source table as the same primary key.
Testing if each row in the target table exists in the source table is a substantial overhead,
so you should enable this option only when really needed. DELETE
s in the
target table are batched according to the -batchSize
setting of the
WbCopy
command. To increase performance, you should enable batching
for the whole process.
Internally the rows from the source table are checked in chunks, which means that
SQL Workbench/J will generate a SELECT
statement that contains
a WHERE
condition for each row retrieved from the target table.
The default chunk size is relatively small to avoid problems with large SQL statements.
This approach was taken to minimize the number of statements sent to the server.
The automatic fallback from update,insert
or insert,update
mode to insert
mode applies for synchronizing tables using WbCopy
as well.
WbCopy -sourceProfile=ProfileA -targetProfile=ProfileB -sourceTable=the_table -targetTable=the_other_table;
This example will copy the data from the tables in the source database to the corresponding tables in the target database. Rows that are not available in the source tables are deleted from the target tables.
WbCopy -sourceProfile=ProfileA -targetProfile=ProfileB -sourceTable=* -mode=update,insert -syncDelete=true;
WbCopy -sourceProfile=ProfileA -targetProfile=ProfileB -sourceTable=the_table -sourceWhere="lastname LIKE 'D%'" -targetTable=the_other_table;
This example will run the statement SELECT * FROM the_table WHERE lastname like 'D%'
and copy all corresponding columns to the target table the_other_table
.
This example copies only selected columns from the source table. The column names in the two tables do not match and a column mapping is defined. Before the copy is started all rows are deleted from the target table.
WbCopy -sourceProfile=ProfileA -targetProfile=ProfileB -sourceTable=person -targetTable=contacts -deleteTarget=true -columns=firstname/surname, lastname/name, birthday/dob;
When using a query as the source for the WbCopy
command, the column
mapping is specified by simply supplying the order of the target columns as they appear
in the SELECT
statement.
WbCopy -sourceProfile=ProfileA -targetProfile=ProfileB -sourceQuery="SELECT firstname, lastname, birthday FROM person" -targetTable=contacts -deleteTarget=true -columns=surname, name, dob;
This copies the data based on the SELECT statement into the table CONTACTS
of the target database. The -columns
parameter defines that the first column
of the SELECT (firstname) is copied into the target column with the name surname
,
the second result column (lastname) is copied into the target column name
and the
last source column (birthday) is copied into the target column dob
.
This example could also be written as:
WbCopy -sourceProfile=ProfileA -targetProfile=ProfileB -sourceQuery="SELECT firstname as surname, lastname as name, birthday as dob FROM person" -targetTable=contacts -deleteTarget=true