R
Ravi
Hi All,
I am trying to execute a select statement using the DBI module
of perl in a for loop. I am getting a strange behaviour, the select
statement is excuting correctly only for the last element in the for
loop.
I am including the portion of the code :
#Get the connection to the database
my $dbh = &getConnection();
my @acodes;
my @bcodes = ('ADADADA', 'ADASDASDAS', 'BBBBBB', 'CCCCCCC');
#Create an SQL statement for getting the list of acodes
my $sql = $dbh->prepare("SELECT DISTINCT Vial.ACODE from VIAL
WHERE VIAL.BCODE = ?");
my @row;
my $bcode;
$i = 0;
for($i=0; $i<@bcodes; $i++){
print "i :".$i."\n";
$sql->bind_param(1, $bcodes[$i]);
$sql->execute();
$acodes[$i] = "";
while(@row = $sql->fetchrow_array){
$acodes[$i] = $row[0];
}
}
#Close the connection to the databse
&closeConnection($dbh);
When I try to print the @acodes, it only has an entry for the
corresponding last element of the @bcode.
When I remove the for loop in the above code, and hardcode
the elements present in @bcodes it works fine.
It would be a great help, if anyone can let me know what I am doing
wrong.
Thanks in advance.
Ravi.
I am trying to execute a select statement using the DBI module
of perl in a for loop. I am getting a strange behaviour, the select
statement is excuting correctly only for the last element in the for
loop.
I am including the portion of the code :
#Get the connection to the database
my $dbh = &getConnection();
my @acodes;
my @bcodes = ('ADADADA', 'ADASDASDAS', 'BBBBBB', 'CCCCCCC');
#Create an SQL statement for getting the list of acodes
my $sql = $dbh->prepare("SELECT DISTINCT Vial.ACODE from VIAL
WHERE VIAL.BCODE = ?");
my @row;
my $bcode;
$i = 0;
for($i=0; $i<@bcodes; $i++){
print "i :".$i."\n";
$sql->bind_param(1, $bcodes[$i]);
$sql->execute();
$acodes[$i] = "";
while(@row = $sql->fetchrow_array){
$acodes[$i] = $row[0];
}
}
#Close the connection to the databse
&closeConnection($dbh);
When I try to print the @acodes, it only has an entry for the
corresponding last element of the @bcode.
When I remove the for loop in the above code, and hardcode
the elements present in @bcodes it works fine.
It would be a great help, if anyone can let me know what I am doing
wrong.
Thanks in advance.
Ravi.