| |
| ||||||
Parameterizing SQL statements in the Kettle Table Input step: Variables vs ParametersThis is a discussion on Parameterizing SQL statements in the Kettle Table Input step: Variables vs Parameters within the DWH Tip Feeds forums, part of the Data Warehousing Tips and Techniques category; I get this question on a regular basis, so I figured I might as well blog it, in the hope it will be useful for others. Here goes: Let's say ... |
![]() |
| | LinkBack | Thread Tools | Search this Thread | Display Modes |
| | #1 |
| News Bot Join Date: Nov 2007
Posts: 15,067
![]() | I get this question on a regular basis, so I figured I might as well blog it, in the hope it will be useful for others. Here goes: Let's say that I want to delete all records that match an id from a set of tables. The table names come in as rows into the Execute SQL Script step (check execute for every row). Next I write:(Although this particular question focuses on the "Execute SQL Script" step, it also applies to the "Table Input" step, and probably a few more steps I can't recall right now.) The parameters grid can be used for prepared statement value placeholders. In the SQL statement these placeholders are denoted as questionmarks (?). These are positional parameters: they get their value from those fields in the incoming stream that are entered in the parameters grid, in order. Here's an example of the correct usage of these placeholders: DELETE FROM myTable WHERE id = ? Here, the ? in the WHERE clause will be bound to the value of the first field from the incoming stream entered in the parameters grid. Because there is only one such placeholder, there can be only one field in the parameters grid. An important thing to realize is that these parameters can only be used to parameterize value expressions. So, this kind of parameter does not work for identifiers, nor do they work for structural elements of the SQL statement, such as keywords. So this kind of parameter cannot be used to parameterize the table name which seems to be the intention in the original example posed in the question. There is a way to parameterize the structural elements of the SQL statement as well as the parameters. You can apply variable substitution to the SQL statetment. Kettle Variables can be defined by a Set Variables step, or by specifying parameters at the transformation level. They get their value from "the environment": for example, parameters get their value initially when the transformation is started, and regular variables are typically set somewhere in the job that is calling your transformation. In text fields, including the SQL textarea of the Table input step or the Execute SQL Script step, you denote those variables with this syntax: ${VARIABLE_NAME}. So to parameterize the table name we could use something like this: DELETE FROM ${TABLE_NAME} In order to force kettle to apply variable substitution to the SQL statement, you have to check the "variable substitution" checkbox. If this checkbox is checked, then all variables are simply substituted with their (string)value during transformation initialization. This is a lot like the way macro's are substituted by the pre-processor in C/C++ code. When comparing variables with parameters, two important things should be mentioned here:
Finally, here's a screenshot that summarizes these different ways to parameterize SQL statements in kettle: ![]() If you want to read more about this topic, it's covered in both our books Pentaho Solutions and Pentaho Kettle Solutions. Another title you might be interested in is Maria Roldan's Pentaho 3.2 Data Integration: Beginner's Guide. More from Roland Bouman's Blog ... |
| | |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 10,000 observations and 100,000 parameters: what to do? | admin | Analytic News Feeds | 0 | 12th September 2010 07:12 PM |
| Google Analytics on site targeting based on custom variables | admin | Presentation News Feeds | 0 | 8th June 2010 12:23 PM |
| Reform of input taxes too hard for now | Latest News Headlines | 2010 Q2 News Headlines | 0 | 3rd May 2010 12:12 PM |
| Report Variables and Group Variables in Reporting Services 2008 | Latest News Headlines | Microsoft News and Views | 0 | 25th November 2009 01:49 PM |
| ShortRead: a bioconductor package for input, quality assessment and exploration of hi | admin | Analytic News Feeds | 0 | 18th November 2009 11:38 AM |
| | |
| | |