Below is the Perl script that I have written
my $COUNT_1;
my $parameter1 = 'PU_CLERK';
$COUNT_1 = `sqlplus -s hr/password\@dbname\@sql_script.sql $parameter1`;
SQL SCRIPT:
select count(*) from employees
where job_id <> '&1'
and salary > 9000
and commission_pct is not null
order by first_name desc
/
exit;
When I run this query by passing the argument &1 it is giving me a string with an error message. But when I run the same query by hardcoding I'm getting the output properly (the count is 15 which is the correct answer).
select count(*) from employees
where job_id <> 'PU_CLERK'
and salary > 9000
and commission_pct is not null
order by first_name desc
/
exit;
I'm not able to understand where I'm going wrong. How do I pass parameters in Perl. We used to do the same way in shell script and it was working absolutely fine.
EDIT:
This is the error message im getting
perl call_sql.pl
value of first variable isold 2: whe
re job_id <> '&1'
new 2: where job_id <> 'PU_CLERK'
15
So its basically not printing the 15 value its printing all those string also when i use '&1' in my sql script
EDIT2:
Hi Guys finally it is working. In my sql code instead of giving '&1' i gave '1ドル' Now i want to know is 1ドル of some significance in Perl? Thanks..
2 Answers 2
I don't know the answer to your current problem, but using the DBI module is the better solution, so I wrote a sample script to get you started. You may need to tweak some things to get it to work.
use strict;
use warnings;
use DBI;
my $dbname = "mydb";
my $user = "foo";
my $passwd = "bar";
my $dbh = DBI->connect("dbi:Oracle:$dbname", $user, $passwd)
or die $DBI::errstr;
my $parameter1 = 'PU_CLERK';
my $statement = "select count(*) from employees
where job_id <> ?
and salary > 9000
and commission_pct is not null
order by first_name desc";
my $sth = $dbh->prepare($statement) or die $dbh->errstr;
$sth->execute($parameter1) or die $sth->errstr;
while (my $row = $sth->fetchrow_arrayref) {
print "@$row"; # or whatever you want to do with it
}
$dbh->disconnect or warn $dbh->errstr;
Comments
This has nothing with perl.
Proof: make an shell script, say mytest.sh with the next content:
#!/bin/bash
echo "0ドル: Got $# args" >&2 #to stderr
i=0
for arg
do
let i++
echo "0ドル: arg($i)=$arg=" >&2 #to stderr
done
echo "15" #result to stdout
make it executable, with chmod 755 mytest.sh
Now modify your perl script as:
my $COUNT_1;
my $parameter1 = 'PU_CLERK';
$COUNT_1 = `./mytest.sh -s hr/password\@dbname\@sql_script.sql $parameter1`;
print "script returned: $COUNT_1\n";
run it
$ perl script.pl
result:
./mytest.sh: Got 3 args
./mytest.sh: arg(1)=-s=
./mytest.sh: arg(2)=hr/password@dbname@sql_script.sql=
./mytest.sh: arg(3)=PU_CLERK=
script returned: 15
e.g. the perl
- correctly run the external script
- correctly passes the arguments to it
- so, search for the error in the
sqlplusdoccumentation...
DBImodule instead?$COUNT_1 = sqlplus -s hr/password\@dbname\@sql_script.sql PU_CLERK?sqlplus -s hr/password...script.sql PU_CLERKfrom the command line?