in reply to Looking for neater solution with dynamic query

If you are constructing the query yourself (as opposed to some of the above suggestions), you can still use placeholders. Here's a generic format (very similar to the SQL::Abstract approach):
my $sql = "SELECT ... FROM table WHERE 1=1 "; my @bind; if( $query{env} ){ $sql .= " AND upper(env) = upper(?) "; push @bind, $query{env}; } if( $query{component} ){ $sql .= " AND upper(component) = upper(?) "; push @bind, $query{component}; } ... my $sth = $dbh->prepare($query); $sth->execute(@bind); # or use one of the $dbh->selectXXXX($query, {}, @bind) methods of DB +I

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

    I really like this. To push those bindings into an array does the trick. In this way, I am taking the full benefit of database binding, and yet allow me to construct the query on fly.

    I am going with this solution. Thank you very much!

    Oh that 1=1 is neat.

      Oh that 1=1 is neat.

      TRUE should work too.

      the lowliest monk

        But has 1 char more.