Perl DBI - Set schema name as bind variable in query -


i'm writing script executes same merge statement across several different databases. tables names , columns same in each schema connecting to, schema names different. rather define 2 separate queries, prefer define 1 merge statement string schema name set '?', , pass in needed schema name during execution:

my $hostgroup_merge = "merge ?.hostgroups .... ";

and later:

my $dev_schema  = 'dev'; $prod_schema = 'prod';  #do merge in dev schema $dbh = dbi->connect($dev_server,$user,$pass); $sth = $dbh->prepare($hostgroup_merge); $sth->execute($dev_schema); $sth->finish(); $dbh->disconnect;  #now same merge prod schema $dbh = dbi->connect($prod_server,$user,$pass); $sth = $dbh->prepare($hostgroup_merge); $sth->execute($prod_schema); $sth->finish(); $dbh->disconnect; 

this fails invalid table name error:

dbd::oracle::st execute failed: ora-00903: invalid table name (dbd error: error possibly near <*> indicator @ char 19 in ' merge :p1<*>.hostgroups ...

however, seems picking bind variable fine, since spits out after query:

" paramvalues: :p1='dev']

i know bind variable that's causing problem, because when define separate merge statements , hard-code schema names in, work fine.

since thing that's changing variable in beginning, hack query defining non-changing part of statement in string, , concatenating rest:

my $dev_merge = 'merge ' . $dev_schema . $merge_statement;

and run prepare on that, there legitimate way pass in name via query? if so, can point out what's going wrong here?

select * table x = ? 

is equivalent to

select * table x = 'dev' 

so

merge ?.hostgroups ....  

is equivalent to

merge 'dev'.hostgroups ... 

while want

merge dev.hostgroups ... 

use

"merge ".$dbh->quote_identifier($schema).".hostgroups ..." 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -