http://www.perlmonks.org?node_id=876229

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?


s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

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

    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.

    yours,
    Michael

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

      Thanks to both of you.


      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
        Howdy!

        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.

        yours,
        Michael
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]