<?xml version="1.0" encoding="windows-1252"?>
<node id="1011687" title="DBI indexable placeholder" created="2013-01-04 12:41:47" updated="2013-01-04 12:41:47">
<type id="115">
perlquestion</type>
<author id="1010210">
grump-</author>
<data>
<field name="doctext">
I have some queries that require inputting the same data in multiple places. Previously I have just been using question marks and forcing the client to send the same data as many times as the question marks appear. If two question marks requiring the same input, then send the input twice. But I have started using the custom or named placeholders and I have noticed that I can bind in a pseudo index able way. This code works against an Oracle database (with real query, this one hacked for simplicity sakes):
&lt;code&gt;
#!/usr/bin/perl
use DBI;
use Data::Dumper;

my $dbh = DBI-&gt;connect( dsn, user, pass, { RaiseError =&gt; 1 } );

my $query = qq{
select * from blah_table where col1 = :p0
  union select * from blah_table where col1 = :p0
    union select * from blah_table where col1 = :p0
  };

my $dbhq = $dbh-&gt;prepare( $query );

my @args = ('foo_param');

while( $query =~ /(:p(\d+))/g ) {   #$1 will be :p0, $2 will be 0
  $dbhq-&gt;bind_param($1, @args[$2]); #':p0 gets @args[0]
}

$dbhq-&gt;execute();

print Dumper($dbhq-&gt;fetchall_arrayref());
&lt;/code&gt; 

I hate to just accept that 'if it works, use it' because I might end up down the road in trouble having used poor practice. If someone has done this successfully for some time I would like to know. Even if it does work, is it good practice? I had the thought that if I could test multiple overlapping placeholders successfully (e.g. change query and args to: &lt;code&gt;...
my $query = qq{
select * from blah_table where col1 = :p0
  union select * from blah_table where col1 = :p0
    union select * from blah_table where col1 = :p1
      union select * from blah_table where col1 = :p0
        union select * from blah_table where col1 = :p1
  };

...
my @args = ('foo_param0', 'foo_param1');
...&lt;/code&gt; ) that might prove to me that it is o.k. to use but even so, some input from experts would be nice. Any input would be welcome. Thanks!</field>
</data>
</node>
