Search code examples
talend

How to set context variable from database


I'm trying to set a context variable from a row in a oracle database. I have the table in the database already set up like so:

key     | variable
name    | BigDecimal

I also have created a BigDecimal context variable. How do I link the context variable to the database so it can be used in a SQL query?


Solution

  • If you have a table with schema (variable_name, variable_value) and each row id dedicated to a single job, then I recommand you to add a third column so it will be (variable_name, variable_value, job_name), then with simple single query you will get the row of the current job:

    select variable_name, variable_value from context_table where job_name = '"+jobName+"';
    --jobName is varibale global exists in each Talend by defaults
    

    enter image description here

    If your table is not modeled to serve context, but you need a specefic value from that table to be loaded into context, you have to execute the query using TOracleRow and pass the result to tJavaRow that have the code below:

    context.myVariable = input_row.myColumn
    

    If you want to use that context variable in other queries, your query will be like this:

    "select a, b, c from table1 where a = '"+context.myVariable+"'"