DBExecute
Short Description
DBExecute executes specified SQL/DML/DDL statements against a database connected using the JDBC driver. It can execute queries, transactions, call stored procedures or functions.
Input parameters can be received through the single input port and output parameters or result set are sent to the first output port. Error information can be sent to the second output port.
Hint!
For executing INSERT/DELETE/UPDATE SQL queries operating on single records read from input port of the component, consider using DatabaseWriter instead, as it is optimized for these types of queries.
COMPONENT | SAME INPUT METADATA | SORTED INPUTS | INPUTS | OUTPUTS | EACH TO ALL OUTPUTS | JAVA | CTL | AUTO-PROPAGATED METADATA |
---|---|---|---|---|---|---|---|---|
DBExecute | - | x | 0-1 | 0-2 | - | x | x | x |
Ports
PORT TYPE | NUMBER | REQUIRED | DESCRIPTION | METADATA |
---|---|---|---|---|
Input | 0 | [1] | Input records for stored procedure or the whole SQL commands. | Any |
Output | 0 | [2] | Update count and the executed statement. Output parameters and result set of stored procedures. | Any |
1 | x | For error information | Based on input metadata |
[1] Input port must be connected if the Query input parameters attribute is specified or if the whole SQL query is received through the input port.
[2] The output port must be connected if the Query output parameters or the Return set output fields attribute is specified.
Metadata
DBExecute propagates input metadata to the first output port if not calling a stored procedure. It adds two output fields: updateCount
and statement
.
DBExecute has no metadata template.
Metadata on both output ports may contain any number of fields from input (same names and types). Input metadata are mapped automatically according to their name(s) and type(s).
Standard output metadata may contain two additional fields: updateCount
and statement
(case insensitive, so UpdateCount
or UPDATE_COUNT
also works). If present, these fields are populated with the number of updated rows and the executed statement, respectively. This feature only works if the Call as stored procedure attribute is set to false.
Error output metadata may contain additional fields for error information. The two error fields may have any names and must be set to the following Autofilling Functions: ErrCode
and ErrText
.
DBExecute Attributes
ATTRIBUTE | REQ | DESCRIPTION | POSSIBLE VALUES |
---|---|---|---|
Basic | |||
DB connection | yes | ID of the DB connection to be used. | |
SQL query | [1] | An SQL query defined in the graph. Contains SQL/DML/DDL statement(s) that should be executed against database. If a stored procedure or function with parameters should be called or if output data set should be produced, the form of the statement must be the following: {[? = ]call procedureName([?[,?,[…]])}. (Do not forget to enclose the statement in curly brackets!) At the same time, if the input and/or the output parameters are required, corresponding attributes are to be defined for them (Query input parameters, Query output parameters and/or Result set output fields, respectively). If the query consists of multiple statements, they must be separated from each other by specified SQL statement delimiter. Statements will be executed one by one. | |
Query URL | [1] | One of these two options: Either the name of an external file, including path, defining the SQL query with the same characteristics as described in the SQL query attribute, or the File URL attribute string that is used for port reading. For details, see SQL Query Received from Input Port below. | |
Query source charset | Encoding of external file specified in the Query URL attribute. | E.g. UTF-8 | <other encodings> | |
SQL statement delimiter | A delimiter between individual SQL statements in the SQL query or Query URL attribute. Default delimiter is a semicolon. | ";" (default) | other character | |
Print statements | By default, SQL commands are not printed. If set to true , they are sent to stdout. | false (default) | true | |
Transaction set | Specifies whether the statements should be executed in transaction. For more information, see Transaction Set below. Is applied only if the database supports transactions. | SET (default) | ONE | ALL | NEVER_COMMIT | |
Advanced | |||
Call as stored procedure | By default, SQL commands are not executed as stored procedure calls unless this attribute is switched to true . If they are called as stored procedures, JDBC CallableStatement is used. See Calling Stored Procedures and Functions below. | false (default) | true | |
Query input parameters | Used when a stored procedure/function with input parameters is called. It is a sequence of the following type: 1:=$inputField1;…;n:=$inputFieldN . The value of each specified input field is mapped to the corresponding parameter (whose position in SQL query equals to the specified number). This attribute cannot be specified if SQL commands should be received through the input port. | ||
Query output parameters | Used when a stored procedure or function with output parameters or return value is called. It is a sequence of the following type: 1:=$outputField1;…;n:=$outputFieldN . Value of each output parameter (specified by its position in the SQL query) will be written to the specified field. If the function returns a value, this value is represented by the first parameter. Use "result_set" to denote output parameters that return data sets, typically by returning a cursor, for example "2:=result_set". See Calling Stored Procedures and Functions below. | ||
Result set output fields | If a stored procedure or function returns a set of data, its output will be mapped to given output fields. The attribute is expressed as a sequence of output field names separated from each other by a semicolon. See Calling Stored Procedures and Functions below. | ||
Deprecated | |||
Error actions | The definition of an action that should be performed when the specified query throws an SQL Exception. See Return Values of Transformations. | ||
Error log | The URL of the file to which error messages for specified Error actions should be written. If not set, they are written to Console. |
[1] One of these must be set. If both are specified, Query URL has higher priority.
Details
SQL Query Received from Input Port
SQL query can also be received from the input port.
In this case, two values of the Query URL attribute are allowed:
-
SQL command is sent through the input edge.
The attribute value is:port:$0.fieldName:discrete
.
Metadata of this edge has neither default delimiter, nor record delimiter, but EOF as delimiter must be set to true. -
The name of the file containing the SQL command, including the path, is sent through the input edge.
The attribute value is:port:$0.fieldName:source
.
For more details about reading data from input port, see Input Port Reading.
Transaction Set
Options are the following:
-
One statement
The commit is performed after each query execution. -
One set of statements
All statements are executed for each input record. The commit is performed after a set of statements.
For this reason, if an error occurs during the execution of any statement for any of the records, all statements are rolled back for such a record. -
All statements
The commit is performed after all statements, only.
For this reason, if an error occurs, all operations are rolled back. -
Never commit
The commit or rollback may be called from other component in a different phase. There is no automatic rollback.
Warning!If no error occurs, the connection closure results in
autocommit
even if Never commit is selected. If you need torollback
, the rollback must be called before autocommit on session’s termination.
If you want to use the Never commit option and performcommit
orrollback
from another component in another phase, set Thread-safe connection in advanced Connection settings tofalse
. Otherwise, each component will have different connection and autocommit will be performed at the end of processing of particular components.
Calling Stored Procedures and Functions
The following table summarizes basic configuration examples to call stored procedures, setting input parameters and obtaining output values in the form of output parameters, return values or cursors.
SQL OBJECT DECLARATION | OUTPUT | SQL QUERY | QUERY INPUT PORT PARAMETERS | QUERY OUTPUT PORT PARAMETERS | RESULT SET OUTPUT FIELDS |
---|---|---|---|---|---|
PROCEDURE remove_customer(id IN NUMBER) | None | { call remove_customer(?) }; | 1:=$customer_id; | ||
PROCEDURE find_customer_name_by_id(id IN NUMBER, name OUT VARCHAR) | Parameter | { call find_customer_name_by_id(?, ?) }; | 1:=$customer_id; | 2:=$customer_name; | |
PROCEDURE get_newest_customer(c_cursor OUT SYS_REFCURSOR) | Cursor | { call get_newest_customer(?) }; | 1:=result_set; | customer_id;custom_name;customer_address | |
PROCEDURE get_customer(id IN NUMBER, c_cursor OUT SYS_REFCURSOR) | Cursor | { call get_customer(?, ?) }; | 1:=$customer_id; | 2:=result_set; | customer_id;custom_name;customer_address |
FUNCTION get_customer_name_by_id(id IN NUMBER) RETURN VARCHAR | Value | { ? = call get_customer_name_by_id(?) }; | 2:=$customer_id; | 1:=$customer_name; |
Examples
Creating a database table with DBExecute
This example shows a basic use case of this component.
Create a database table rivers with two columns: name and length.
Solution
Create a database connection RiversConnection. See Creating Internal Database Connections.
Set the DB Connection to RiversConnection connection.
Enter the CREATE statement into the SQL Query attribute.
-
ATTRIBUTE
DB Connection
VALUE
RiversConnection -
ATTRIBUTE
Query URL
VALUE
CREATE TABLE rivers (
name VARCHAR(50) NOT NULL,
length INTEGER
);
If you use one graph to create a database table and insert data to it, do no forget to put DBExecute and the component inserting the data into different phases. DBExecute should be in a lower phase and DatabaseWriter (or other component writing to the database) in a higher phase.
Executing multiple queries
DBExecute is not limited to execution of one SQL query.
Create table rivers as in the previous example. If the table exists, drop it first.
Solution
-
ATTRIBUTE
DB Connection
VALUE
RiversConnection -
ATTRIBUTE
Query URL
VALUE
DROP TABLE IF EXISTS rivers;
CREATE TABLE rivers (
name VARCHAR(50) NOT NULL,
length INTEGER
);
Do not forget to end statements with semicolons.
Note: some databases (e.g. Oracle) do not understand the IF EXISTS
part of the query. If you use a database that does not support IF EXISTS
, change the first line of the query to DROP TABLE rivers;
.
Creating a stored procedure
Create a stored procedure that inserts data into the products table. The table has been created with the following query:
CREATE TABLE products (
product_name VARCHAR(40) NOT NULL
);
Solution
Create a database connection ProductConnection.
-
ATTRIBUTE
DB Connection
VALUE
ProductConnection -
ATTRIBUTE
Query URL
VALUE
CREATE OR REPLACE FUNCTION add_product(param VARCHAR(40))
RETURNS VOID AS '
BEGIN
INSERT INTO products (product_name) VALUES(param);
END;
' LANGUAGE plpgsql;
The example has been tested with PostgreSQL 9.2 and 14.4.
Getting errors
This example shows a way to get errors related to an SQL query from the DBExecute component to send it to the next component for further processing.
The graph uses DBExecute to create a database table bricks. The bricks table contains details on products from our supplier. The table might exist or we could not have permission to create a table and we would like to receive the errors from the database.
Solution
Create a database connection ProductConnection.
-
ATTRIBUTE
DB Connection
VALUE
ProductConnection -
ATTRIBUTE
Query URL
VALUE
CREATE TABLE bricks (
id INTEGER PRIMARY KEY NOT NULL,
length integer,
width integer,
height integer,
weight integer
);
Connect an edge to the second output port of DBExecute.
Assign metadata to the edge. The metadata should contain a String
field that has Autofilling set to ErrCode
.
Best Practices
Upload and Download of Data
Do not use the DBExecute component for INSERT and SELECT statements. For uploading data to a database, please use the DatabaseWriter component. Similarly, for downloading use the DatabaseReader component.
Transferring data within a database
The best practice to load data from one table to another in the same database is to do it inside the database. You can use the DBExecute component with a query like this:
insert into my_table select * from another_table
because pulling data out from the database and putting them in is slower as the data has to be parsed during the reading and formatted when writing.
Charset
If the query is specified in an external file (with Query URL), we recommend users to explicitly specify Query source charset.
Troubleshooting
Boolean and Oracle JDBC
Calling arguments or return values of the PL/SQL RECORD, BOOLEAN, or table with non-scalar elements are not supported by Oracle JDBC Drivers. See Oracle JDBC Reference
As a workaround, you can create a wrapper procedure. See Wrapper procedures
See also
DatabaseReader
DatabaseWriter
Common properties of components
Specific attribute types
Updated 8 months ago