Iterating on DB tables and deleting their content using a user-defined SQL template
The following Java scenario creates a three-component Job that iterates on given table names from a MySQL database using a WHERE clause. It then deletes the content of the tables directly on the DBMS using a user-defined SQL template.
For advanced use, start with creating a connection to the database that contains the tables you want to empty of their content.
-
In the Repository tree view, expand Metadata and right click DB Connections to create a connection to the relevant database and to store the connection information locally.
For more information about Metadata, see Managing metadata in Talend Studio.
Otherwise, drop a tMysqlConnection component in the design workspace and fill the connection details manually.
-
Drop the database connection you created from the Repository onto the design workspace.
The Components dialog box displays.
-
Select tMysqlConnection and click OK.
The tDBConnection_1 components displays on the design workspace with all connection details automatically filled in its Basic settings view.
-
Drop the following two components from the Palette onto the design workspace: tMysqlTableList (displayed as tDBTableList_1) and tSQLTemplate.
-
Connect tDBConnection_1 to tDBTableList_1 using an OnSubjobOk link.
-
Connect tDBTableList_1 to tSQLTemplate using an Iterate link.
-
If needed, double-click tDBConnection_1 to display its Basic settings view and verify the connection details.
In this example, we want to connect to a MySQL database called examples.
-
In the design workspace, double-click tDBTableList_1 to display its Basic settings view and define its settings.
-
On the Component list, select the relevant MySQL connection component if more than one connection is used.
-
Enter a WHERE clause using the right syntax in the corresponding field to iterate on the table name(s) you want to delete the content of.
In this scenario, we want the Job to iterate on all the tables whose names start with ex.
-
In the design workspace, double-click tSQLTemplate_1 to display its Basic settings view and define its settings.
-
In Database Name field, enter the name of the database containing the tables you want to process.
-
From the Component list drop-down list, select the relevant MySQL connection component if more than one connection is used.
-
Click in the Table name field and press Ctrl + Space to access the global variable list.
-
From the global variable list, select tDBTableList_1_CURRENT_TABLE.
To create the user-defined SQL template:
-
In the Repository tree view, expand SQL Templates and MySQL in succession.
-
Right-click UserDefined and select Create SQLTemplate from the drop-down list.
The New SQLTemplate wizard opens.
-
Enter a name for the new SQL template and fill in the other fields If needed and then click Finish to close the wizard.
An SQL pattern editor opens on the design workspace.
-
Delete the existing code and enter the code necessary to carry out the desired action, deleting the content of all tables which names start with "ex" in this example.
In the SQL template code, you must use the correct variable name attached to the table name parameter (__TABLE-NAME__ in this example). To display the variable name used, put your pointer in the Table Name field in the basic settings of the tSQLTemplate_1 component.
-
Press Ctrl + S to save the new user-defined SQL template.
The next step is to add the new user-defined SQL template to the SQL template list of the tSQLTemplate_1 component.
To add the user-defined SQL template to the SQL template list:
-
In the Component view of tSQLTemplate_1, click the SQL Templates tab to display the SQL Template List.
-
Click the Add button and add two SQL template lines.
-
Click in the first line to display a drop-down arrow and then click the arrow to display the SQL template list.
-
Select in the list the user-defined SQL template you already created.
-
Make sure that the SQL template in the second line is Commit.
-
Save your Job and press F6 to execute it.
All tables in the MySQL examples database whose names begin with ex are emptied from their content.