Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
The stupid question is the question not asked
 
PerlMonks  

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?


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

Comment on DBD and bind_param (Oracle)
Select or Download Code
Re: DBD and bind_param (Oracle)
by mje (Deacon) 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 herveus (Parson) 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 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://876229]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2014-04-20 17:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls