in reply to Re^2: DBI parameterized database query with comma seperated list
in thread DBI parameterized database query with comma seperated list
Don't do that. Dynamic SQL is bad news.
Every RDBMS has this issue, because stored procedures are not able to use a dynamic list. The way to work this out changes by RDBMS, but all of them are ways it split the values in SQL itself.
Anyway, a simple answer would be to use a recursive CTE to split the values into separate records. Once that is done, IN() or EXISTS can SELECT from the entire CTE.
Here's the basic idea. I quickly looked at mysql functions to write this, but have not tested it at all. Hopefully it shows the basic idea:
WITH RECURSIVE split_csv(x, rest) AS ( SELECT SUBSTRING_INDEX(?, ',', 1), SUBSTRING(?, FROM INST +R(?, ',') + 1) UNION ALL SELECT SUBSTRING_INDEX(rest, ',', 1), SUBSTRING(rest, FROM I +NSTR(?, ',') + 1) FROM split_csv) SELECT something FROM atable WHERE data in (SELECT x FROM split_csv);
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: DBI parameterized database query with comma seperated list
by kennethk (Abbot) on Dec 21, 2015 at 01:34 UTC | |
by Pope-O-Matik (Pilgrim) on Dec 21, 2015 at 11:28 UTC | |
by kennethk (Abbot) on Dec 21, 2015 at 23:23 UTC | |
by Pope-O-Matik (Pilgrim) on Dec 22, 2015 at 04:44 UTC | |
Re^4: DBI parameterized database query with comma seperated list
by Apero (Scribe) on Dec 21, 2015 at 02:06 UTC | |
by Pope-O-Matik (Pilgrim) on Dec 21, 2015 at 11:42 UTC |