0

Hi i have a sql file(test.sql) something as simple as ,

select emp_id from employees 
where country='xxx'
and department='sales'

i know how to call this sql using a shell script, we normally use

#/bin/ksh

sqlplus $login $password test.sql

so my shell will call this sql and i spool this into a file. But how to do the same using a perl script.

I'm totally new to perl script and any help is greatly appreciated.

2 Answers 2

0

You won't be able to directly execute the sql file without first writing a perl script which takes the path to your sql file as a parameter then executes it. Here is a question which explains how to do (in perl):

Execute SQL file in Perl

However, if you just want to execute the sql file there are easier ways to do that. Feel free to clarify exactly what you are trying to accomplish other than executing this with perl instead of ksh - we might be able to provide more insight :).

Sign up to request clarification or add additional context in comments.

Comments

0

On a very basic level you could just call a system command as shown below.

Assuming your Oracle environment variables are set up in the user and sqlplus can be found on the $PATH

my $login     = "fred";
my $password  = "secretword";
my $sqlfile   = "/path/to/test.sql";

my @sql_array = system("sqlplus $login $password $sqlfile");

I chose to store the results in an array so that you could traverse each element (row) in turn

foreach my $row ( @sql_array )
{
    print $row;
}

There may be much better ways to do this but keeping it basic when learning Perl, I always found the system() command to be a good friend. Using Perl arrays is my chosen method of reading in output from a system command.

You may find other bits of spurious data in the array (sqlplus but you can prune these out with regular expressions in the foreach loop.

Comments

Your Answer

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.