Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

DBD and bind_param (Oracle)

by Skeeve (Vicar)
on Dec 09, 2010 at 10:35 UTC ( #876229=perlquestion: print w/replies, xml ) Need Help??
Skeeve has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use a bind variable in a SELECT ... IN statement, but can't figure out how to do it.

This one works:

SELECT * FROM mytabel WHERE mycolumn = :VAR1

I simply do a

$sth->bind_param(':VAR1', $myvalue);

But trying this:

SELECT * FROM mytabel WHERE mycolumn in (:VAR1)
only works if I bind just one value. Or to be precise: It seems as if it's only possible to supply one value.

So $myvalue="1, 2, 3"; will result in searching for ('1, 2, 3') instead of (1, 2, 3).

is there any way to supply several values for :VAR1?


Replies are listed 'Best First'.
Re: DBD and bind_param (Oracle)
by herveus (Parson) on Dec 09, 2010 at 12:15 UTC

    The short answer is "no".

    The parsed statement will only have one item in the IN list. The only way to get more slots is to have more bind variables as noted above. If you want the advantages of placeholders and bind variables, you are stuck with this limitation. Sorry about that.


      Not the answer I wanted to read but at least I don't need to search any further.

      Thanks to both of you.


        Now, there are other ways to work the problem that retain the use of bind variables. Consider:

        my @values = qw/1 2 3/; my $sql = 'select * from foo where x in (' . join(', ', ('?') x @in_va +lues) . ')'; $dbh->prepare($sql)->execute(@values);

        This preserves the use of placeholders and the benefits therefrom while adapting to the changing number of parameters. You can't avoid constructing the query to fit the data.

Re: DBD and bind_param (Oracle)
by mje (Curate) on Dec 09, 2010 at 10:37 UTC

    Did you try "select * from mytable where mycolumn in (:VAR1,:VAR2,:VAR3)" and binding 3 values?

Re: DBD and bind_param (Oracle)
by andreas1234567 (Vicar) on Dec 10, 2010 at 14:49 UTC
    You could resolve to PL/SQL trickery where you pass a comma separated list of values as a string, which is then parsed and split into a list by a PL/SQL function. YMMV.

    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://876229]
Approved by moritz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2018-05-26 23:48 GMT
Find Nodes?
    Voting Booth?