0

Currently, I am spooling the sqlplus command into a text file but even this is causing me problems as I would like to separate the values by commas. So far It has not worked.

I was hoping something like this would work

 @test = system('sqlplus un/pw@host @test.sql');

The test.sql file contains three statements all returning numbers. If I could save these to the @test array, this would be great.

Any ideas?

2
  • 1
    system does not return standard output, it returns the return value of the system call. To capture output use backticks or qx(), but you will need to escape the @ characters to avoid interpolation. However, if you are querying a database, you might be better off using the DBI module. Commented Jan 22, 2013 at 10:32
  • check this:theunixshell.blogspot.com/2013/01/… .where in here it uses sybase just remove the sybase part and put sqlplus instead of isql.the remaining it stores each line of the result into an array. Commented Jan 22, 2013 at 10:44

2 Answers 2

2

Use the DBI module with DBD::Oracle:

#!/usr/bin/perl
use warnings;
use strict;

use DBI;

my $dbh = DBI->connect("dbi:Oracle:host=host;sid=dbname",
                       $user, $password);

open my $IN, '<', 'test.sql' or die $!;
$/ = ';'; # Queries separated by semicolons, no other semicolons anywhere!
while (my $sql = <$IN>) {
    my $sth = $dbh->prepare($sql);
    $sth->execute;
    my @test = @{ $sth->fetchall_arrayref // [] };
    print "@$_\n" for @test;
}
Sign up to request clarification or add additional context in comments.

Comments

0

The DBI script based is good, but if your point is about the use of sqlplus command in any case, you should take a look at this CPAN module: Expect. It provides you a deeper control of an external command in many and useful ways.

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.