1

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..

asked Sep 16, 2014 at 9:18
15
  • 4
    Why not use the DBI module instead? Commented Sep 16, 2014 at 9:29
  • If you're calling an external program from perl, you can't just type in the command. You need to execute it somehow. See how to execute external programs. Commented Sep 16, 2014 at 9:29
  • 4
    @user2647888 Well, my advice is to use the DBI module. It might take you a while to set up, but then you always have a solid tool to use. Commented Sep 16, 2014 at 10:09
  • 1
    Does it work if you run $COUNT_1 = sqlplus -s hr/password\@dbname\@sql_script.sql PU_CLERK? Commented Sep 16, 2014 at 10:10
  • 1
    Does it run if you do sqlplus -s hr/password...script.sql PU_CLERK from the command line? Commented Sep 16, 2014 at 10:19

2 Answers 2

4

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;
answered Sep 16, 2014 at 11:46
Sign up to request clarification or add additional context in comments.

Comments

3

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 sqlplus doccumentation...
answered Sep 16, 2014 at 10:21

2 Comments

yeah jm666 im running it from windows actaully.
@user2647888 unfortunately i can't make an windows.cmd file to test it

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.