26. SQL Workbench/J to generate DML statements

26.1. Generate DELETE statements with dependencies - WbGenerateDelete
26.2. Generate INSERT statements with dependencies - WbGenerateInsert
[Important]

All SQL Workbench/J specific command can only be used from within SQL Workbench/J

26.1. Generate DELETE statements with dependencies - WbGenerateDelete

The command WbGenerateDelete can be used to generate a SQL script for one or more rows that should be deleted including all rows from referencing tables (if foreign key constraints are defined)

This is also available through the menu item Data → Generate delete script which will generate the delete for the selected row(s) in the current result.

The command supports the following parameters.

ParameterDescription
-table

Specifies the root table of the hierarchy from which to delete the rows.

It is possible to define multiple tables separated by a comma. The table names may contain wildcards. If multiple tables are specified, specifying a column value for the WHERE clause is not supported.

-columnValue

Defines the expression for each PK column to select the rows to be deleted. The value for this parameter is the column name followed by a colon, followed by the value for this column or an expression.

e.g.: -columnValue="person_id:42" will select rows where person_id has the value 42.

You can also specify expressions instead: -columnValue="id:<0" or -columnValue="id:in (1,2,3)".

For a multi-column primary key, specify the parameter multiple times: -columnValue="person_id:100" -columnValue="address_id:200".

This parameter is ignored if more than one table is specified with the -table parameter.

-includeCommit

If true a COMMIT statement will be appended to the generated script.

-outputFile

The file into which the generated statements should be written. If this is omitted, the statements are displayed in the message area.

-appendFile

Valid values: true, false

If true, the statements are appended to an existing file. Otherwise any existing file will be overwritten.

-formatSql

Valid values: true, false

If true, the generated SQL will be formatted using the SQL formatter. If false each statement will be written as a single line.

To generate a script that deletes the person with ID=42 and all rows referencing that person, use the following statement:

WbGenerateDelete -table=person -columnValue="id:42";

To generate a script that deletes any person with an ID greater than 10 and all rows referencing those rows, use the following statement:

WbGenerateDelete -table=person -columnValue="id: > 10";

To generate a script that deletes rows from the film_category where the primary key consists of the columns film_id and category_id:

WbGenerateDelete -table=person -columnValue="film_id: in (1,2,5)" -columnValue="category_id: in (7,3,5);

To generate a script that deletes rows from multiple tables in the correct order:

WbGenerateDelete -table=order*,customer*,invoice*;

26.2. Generate INSERT statements with dependencies - WbGenerateInsert

The command WbGenerateInsert can be used to generate a SQL script that shows the order in which INSERT statements should be executed with respect to declared foreign keys.

The command supports the following parameters.

ParameterDescription
-tables

Specify the list of tables for which the INSERT statements should be generated.

-retrieveFKParents

If this is true, all parent tables for the tables specified by -tables are retrieved before processing the list of tables to sort them in the correct order.

-fullInsert

If this is true the output will be complete INSERT statements, rather than just the list of tables in the correct order.

-file

Specifies an output file to which the result is written instead of displaying it.