Hi Nitin,

2007/2/16, Nitin Aggarwal <[EMAIL PROTECTED]>:
Hi,

I am using bind variables for executing a select query which uses "in"
operator. Following is the prototype of my code:

$query = "select a from tab where b in (:1)";
@list = {"A","B","C","D"};
$list_values = join("','",@list);
$DBI-> connect (..);
$sth -> prepare($query);
$sth-> execute ($list_values);
$array_ref = $sth->fetchrow_arrayref

This does not return me any values.
I suspect the issue is with the usage of passing up the comma separated
values as the parameter to the execute function.


From DBI man page:

      Also, placeholders can only represent single scalar values.  For exam-
      ple, the following statement won't work as expected for more than one
      value:

        "SELECT name, age FROM people WHERE name IN (?)"    # wrong
        "SELECT name, age FROM people WHERE name IN (?,?)"  # two names

I suggest you to check how many items @list has, and construct your
query according that. For example:

@list = qw(A B C D);
$query =
 "select a from tab where b in (" . join( ",", map { "?" } @list ) . ")";

print $query, "\n";

Then you can do this:

$sth->prepare($query);
$sth->execute (@list);

HTH!

--
Igor Sutton Lopes <[EMAIL PROTECTED]>

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to