in reply to Looking for neater solution with dynamic query

While I suspect this might not be that great from the database side, would it be possible to do a query that contained all of the possible (field like ?) statements, preparing that query, then setting the values to '%' if they are not presented? Something such as:

my $query = qq{SELECT * FROM table_name WHERE (field1 LIKE ?) AND (field2 LIKE ?) AND ... AND (fieldn LIKE ?) ORDER BY ...}; my $rv = $sth->prepare($query); my @query_for = ('%') x $number_of_fields; # then replace those elements in @query_for # with values from whereever, where appropriate $sth->execute(@query_for);

Just wondering...

Replies are listed 'Best First'.
Re^2: Looking for neater solution with dynamic query
by pg (Canon) on Aug 24, 2005 at 22:55 UTC

    My concern is: if the database is not smart enough, this could confuse it, and optimization/performance becomes a real issue. I think that this was also the concern you expressed ;-)