Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
This is a sample part of a code I am working on and I have a questions about adding "Place Holders" to the variables "$fromdate" and "$todate" on the SQL query, how could I accomplish something like that? Has anyone here done something like that that could help me with this?
I commented out what I was trying, but it didn't work. Thanks for the help!
if($number eq "all") { $select = ''; if($fromdate || $todate) { $bet = "and CONVERT(CHAR(10),tdate,101) between '$fromda +te' and '$todate'"; #$bet = "and CONVERT(CHAR(10),tdate,101) between '?' and + '?'"; }else{ $bet = 'and tdate = (Select Max(tdate) From mytable) ord +er by tdate desc'; } }else{ $select = "and number='$number'"; if($fromdate || $todate) { $bet = "and CONVERT(CHAR(10),tdate,101) between '$fromd +ate' and '$todate'"; #$bet = "and CONVERT(CHAR(10),tdate,101) between '?' an +d '?'"; }else{ $bet = 'order by tdate desc'; } } my $all = join ( ',', ('?') x @list ); my $sql = $dbh->exec_select( "SELECT tdate,account,name FROM mytable W +HERE number in ($all) $select $bet ", @list); #my $sql = $dbh->exec_select( "SELECT tdate,account,name FROM mytable +WHERE number in ($all) $select $bet ", @list, $fromdate, $todate);

Replies are listed 'Best First'.
Re: Perl using SQL placeholders issue
by moritz (Cardinal) on Aug 23, 2010 at 13:33 UTC
    Instead of
    #$bet = "and CONVERT(CHAR(10),tdate,101) between '?' and '?'";

    try

    my @args; ... $bet = "and CONVERT(CHAR(10),tdate,101) between ? and ?"; # ^ ^ # not quoted push @args, $fromdate, $today;

    And then pass @args to the exec_select call.

    Perl 6 - links to (nearly) everything that is Perl 6.