Skip to content

How to get data from a database with a constructed query? #4566

Answered by hansva
dave-csc asked this question in Q&A
Discussion options

You must be logged in to vote

This is where you hit the limits of what is possible.
You could indeed compose the query insert it in a Hop variable and use that variable to execute the query, but as you have stated this would allow any part of the query to be modified.
Prepared statements on the other hand are a safe way to inject values into a query but they can't be used to modify the statement on runtime.

What I have done in the past if the goal is to enable/disable specific filters in the where clause is adding a void condition

Select * from my_table
where
(field = ? or 1=?)

By providing a 0 or 1 in the second position you essentially enable or disable that condition

Replies: 5 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by dave-csc
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@thadguidry
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants