以下是我编写的Perl脚本
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;
当我通过传递参数&amp; 1来运行此查询时,它会给我一个带有错误消息的字符串。但是当我通过硬编码运行相同的查询时,我正确地得到输出(计数是15,这是正确的答案)。
select count(*) from employees
where job_id <> 'PU_CLERK'
and salary > 9000
and commission_pct is not null
order by first_name desc
/
exit;
我无法理解我哪里出错了。如何在Perl中传递参数。我们过去常常在shell脚本中使用相同的方法,它工作得很好。
编辑:
这是我得到的错误信息
perl call_sql.pl
value of first variable isold 2: whe
re job_id <> '&1'
new 2: where job_id <> 'PU_CLERK'
15
因此,当我在我的sql脚本中使用'&amp; 1'时,它基本上不会打印15值而打印所有那些字符串
EDIT2:
嗨,大家终于有了工作。在我的sql代码而不是'&amp; 1'我给'$ 1'现在我想知道在Perl中有一些重要的1美元?谢谢..
答案 0 :(得分:4)
我不知道您当前问题的答案,但使用DBI
模块是更好的解决方案,因此我编写了一个示例脚本来帮助您入门。您可能需要调整一些内容才能使其正常工作。
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;
答案 1 :(得分:3)
这与perl没什么关系。
证明:制作一个shell脚本,用下一个内容说mytest.sh
:
#!/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
使用chmod 755 mytest.sh
现在将perl脚本修改为:
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";
运行它
$ perl script.pl
结果:
./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。 perl
sqlplus
doccumentation ...