Question

I have code running a SQL query select rec_data from foo where id=?, and I do something with the return value.

I now have an array of IDs @queryIDs;, how can I run the same query and gather the results in order for the values in this array? Thanks

Was it helpful?

Solution

Something like this could work:

my $sql = 'select rec_data from foo where id in (';
$sql .= join ',', ("?") x @queryIDs;
$sql .= ') order by id';

my $sth = $dbh->prepare($sql);

$sth-> execute(@queryIDs);

# rest of code will be the same as you have now

OTHER TIPS

Dynamic SQL is insecure.

To do the same securely, split the list in SQL, which depends on the RDBMS and version. For example, Oracle 11r2 has a good tokenization method via XMLDB which can be found with a simple search, or the MODEL clause, for those ot scared to use it. SQL Server also does XML. Without the RDBMS and version, it is hard to suggest what to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top