In a former programming life in a different programming language, placeholders of the ":placeholder" type were bound by reference to variables in the program. With DBI, we generally use bind_param() to bind by value a copy of some variable. But bind_param_inout binds by reference, so I was thinking what if I go ahead and use that anyway, e.g.:
my @cols = qw(foo bar);
my $sql = <<SQL;
SELECT :foo, :bar FROM dual
SQL
my $sth = $dbh->prepare($sql);
my %hsh;
for (@cols) {
$sth->bind_param_inout( ":$_" => \$hsh{$_}, 0 );
}
# Set constants...
$hsh{foo} = 'abc';
# Set changing values
$hsh{bar} = 123;
$sth->execute();
while (my @arr = $sth->fetchrow_array) {
print "@arr\n";
}
$hsh{bar} = 456;
$sth->execute();
while (my @arr = $sth->fetchrow_array) {
print "@arr\n";
}
$dbh->disconnect();
And it worked (this was for Oracle, thus, the 'FROM dual'). This is, of course, just an example SQL statement, but may also be most useful for others, like INSERT statements...
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.