in reply to How to interpolate sql-output
See all the problems dynamic SQL causes? :) To remove issues and make it more secure, use a prepared query with placeholders, and supply the value upon execution. Further, if the query will be executed multiple times, a prepared query can run a bit faster, as the optimizer is not usually required after the first run.
Anyway, instead of storing queries in a table, why not just make them views (or stored procedures)? That's what they're for. And, it makes running them a lot easier. If worse comes to worse, you can store the name of the view (or stored procedure) in a table for easy finding. Or, perhaps, the name of the script's internal procedure that executes it.
Side comments on the query itself: The query uses an outer query just to get ids and pass them to the inner query that also has id. Why use two tables? And, order by id desc limit 1 to fetch name can be done via an analytical function. How does this look?:
select distinct foo_id key, last_value(value) over ( partition by foo_id order by id ) value from foo_data where foo_id <> ? and aktivity is true and key = 'name';
Update: Query missed the order by id when fetching name. Here it is again with an analytical function.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to interpolate sql-output
by Seq (Novice) on Jan 14, 2015 at 22:01 UTC | |
by chacham (Prior) on Jan 14, 2015 at 22:46 UTC | |
by Seq (Novice) on Jan 14, 2015 at 23:15 UTC | |
by chacham (Prior) on Jan 15, 2015 at 00:25 UTC | |
by Seq (Novice) on Jan 15, 2015 at 01:17 UTC | |
|