in reply to Re^2: Query using more than one array!
in thread Query using more than one array!

Use a loop to build the query string (and the parameters array passed to execute).

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^4: Query using more than one array!
by Anonymous Monk on May 13, 2009 at 12:02 UTC
    Can you show an example with code building the query string for the arrays?
      use strict; use List::MoreUtils qw/zip/; use DBI; my @reg_num = qw/12345 98564 33234 112345 87564 2345678 938566 1234486 + 223456 123488/; my @month = qw/03 01 01 10 05 11 03 05 02 09/; my $sql_part = join ' or ', map {'(reg_num = ? AND month = ?)'} 1 .. @ +reg_num; my $dbh = DBI->connect("DBI:ODBC:$myserver",$u, $p,); my $sth = $dbh->prepare( "SELECT name, last_name, reg_num, month, year FROM members WHERE $sql_part AND year='2009"); $sth->execute(zip @reg_num, @month);

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        What about something like this:

        use strict; use DBI; my @reg_num = qw/12345 98564 33234 112345 87564 2345678 938566 1234486 + 223456 123488/; my @month = qw/03 01 01 10 05 11 03 05 02 09/; my $dbh = DBI->connect("DBI:ODBC:$myserver",$u, $p,); my $sql = "select name, last_name, reg_num, month, year + from members where year='2009' AND ("; for my $i (0 .. $#reg_num) { $sql .= "reg_num = '$reg_num[$i]' AND month = '$month[$i]' "; $sql .= 'OR ' unless $i == $#reg_num; } $sql .= ')'; # don't forget the ) on the end of the SQL my $sth = $dbh->prepare($sql, { RaiseError => 1 }); $sth->execute();


        Let me know what you think?!