Execute SQL script

(warning) PLEASE NOTE: This documentation applies to an earlier version. For the most recent documentation, visit the Pentaho Enterprise Edition documentation site.

Description

You can execute SQL scripts with this step, either once, during the initialization phase of the transformation, or once for every input-row that the step is given. The second option can be used to use parameters in SQL scripts.

Note: When you have an issue, that the SQL is started at the initialization phase of the transformation and not for each row, make sure to check the option "Execute for each row" (see description below).

The following table describes the available options for configuring the Execute SQL script step

Option 

Description 

Step name

Name of the step; This name has to be unique in a single transformation

Connection

Select a database connection to use

SQL script to execute

Specify the SQL to execute. Separate statements by ; and use question marks as place holders for parameter.
Note: This step does not use prepared statements by default, therefor the given parameters have to be enclosed correct. Numeric values do not need to be enclosed, but all others e.g. Strings need to be enclosed with quotes or double quotes depending on the database dialect. (See also the options "Bind parameters?" and "Quote Strings?" since PDI version 5.0.)

Execute for each row?

Select this option to execute the SQL for each incoming row. In this case paramters can be used. When this option is unchecked, the SQL statement is executed at the step initialization phase.

Execute as a single statement

This option does not split the statements by ; and will send the whole SQL to the database.

Variable substitution

In case you want to use variables in the SQL, e.g. ${table_name}, this option needs to be checked.

Bind parameters?

Check this option to bind parameters using prepared statements, otherwise this step will perform a literal string replacement of the parameters. Note: Since PDI version 5.0

Quote Strings?

This option adds quotes around the string according to the database dialect and also escapes special characters like CR, LF and the quote character itself. Note: Since PDI version 5.0

Parameters

The list of used parameters that will replace the question marks in the query in the given order. So the first question mark will be replaced by the first parameter, the second question mark by the second parameter etc.
Note: If you need the same parameter multiple times in your query, you need to use multiple question marks. Using a "Select Values" step you can duplicate field values within the "Select & Alter" tab by selecting the value once and renaming it a second or third time.

Field to contain insert stats

Optional: If you want to get an additional field in our stream with the number or records that where inserted, please define the field name here.

Field to contain update stats

Same as insert stats, but for updated rows.

Field to contain delete stats

Same as insert stats, but for deleted rows.

Field to contain read stats

Same as insert stats, but for read rows.

Notes

IMPORTANT : Because of the scripting/dynamic nature of the step, it does not use prepared statements and thus is not intended to work fast or optimal. For good performance turn to dedicated steps like Table Output (insert into), Table Input (Select), Update, Delete, etc.

For example, if you want to create 5 tables (tab1, tab2, tab3, tab4 and tab5) you could make such a transformation:

 
The SQL script to execute might look like this:

CREATE TABLE tab?
(
a INTEGER
);

The field name to specify as parameter is then the "count" sequence we defined in the second step.

Note: The execution of the transformation will halt when a statement in the script fails.

As extra option, you can return the total number of inserts (INSERT INTO statements), updates (UPDATE table), deletes (DELETE FROM table) and reads (SELECT statements) by specifying the field names in the lower right of the dialog.