Skip to main content
Announcements
Introducing a new Enhanced File Management feature in Qlik Cloud! GET THE DETAILS!
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

[resolved] context values for sql query

Hi.
I want use some context values for sql query.
Query will be something like: select * from table where value in (context.values);
Those context values are stored in a txt file and they are several, like: monday, friday, saturday.
the query returns no values, because it's looking for context like a string.
How can I make i read context like separated values?
Or is there other better solution?

Labels (2)
1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

the example you've been provided with shows you how you construct the statement.
you need to make sure that you're constructing valid SQL.
In this instance, your context value needs to be set to: - "('lisbon', 'london')".
In your question above, you have no space after "in". You need it.
You just need to make sure that you're creating valid SQL.

View solution in original post

5 Replies
Anonymous
Not applicable
Author

Hello!
Anyone?
Thank you!
Anonymous
Not applicable
Author

In your t{DB}Input component, you will notice that the SQL is within quotes. It is basically a Java String. All you need to do is build your query (its good practice to name your columns explicitly by the way) and then concatenate your context variable to the query like below....
"SELECT Col1, Col2, Col3 FROM mytable WHERE talend_context =" +context.yourVal


If you are concatenating a String value, then you will need to add single quotes, for example....
"SELECT Col1, Col2, Col3 FROM mytable WHERE talend_context ='" +context.yourVal +"'"


As I said, all you are doing is building a query String in Java. So as long as your String results in a legitimate SQL query (and your columns match your schema), it will work.
Anonymous
Not applicable
Author

Hi.
Thanks for your answer. I saw an old post from you referring this, but I still have the problem.
My query is: Select city, country from companies where city in ('lisbon','london') - This works in a DBA Manager.
I have the same query like this "Select city, country from companies where city in" +context.city+
on text file that hold the context info, I have: lisbon','london this does not work beacause it's a string, so it's looking for the exact value: lisbon','london
If I put in the text file: 'lisbon','london', it throws an error "remove the quotes"...
So Any help will be appreciated!
Anonymous
Not applicable
Author

the example you've been provided with shows you how you construct the statement.
you need to make sure that you're constructing valid SQL.
In this instance, your context value needs to be set to: - "('lisbon', 'london')".
In your question above, you have no space after "in". You need it.
You just need to make sure that you're creating valid SQL.
Anonymous
Not applicable
Author

Hi.
Now it's ok and I "got the picture".
It works like a string replacement.
Thank you!!