Here's one way to do it, using the "EMP" demonstration table in the standard "scott" schema of Oracle.
$
$ # display the contents of commands.sql
$ cat commands.sql
select deptno from emp where empno = 7369
select deptno from emp where empno = 7499
select deptno from emp where empno = 7521
select deptno from emp where empno = 7566
select deptno from emp where empno = 7654
select deptno from emp where empno = 7698
select deptno from emp where empno = 7782
select deptno from emp where empno = 7788
$
$ # display the contents of test_dbi.pl perl program
$ cat test_dbi.pl
#!perl -w
use DBI;
$dbh = DBI->connect('DBI:Oracle:','scott','tiger',
{RaiseError => 1, AutoCommit => 1});
open(OUTPUT, ">output.txt") or die "Can't open output.txt: $!";
open (FILE, "<commands.sql") or die "Can't open commands.sql: $!";
while (<FILE>) {
chomp($sql = $_);
$dbh->do($sql);
$sth = $dbh->prepare($sql);
$sth->execute();
while (@row = $sth->fetchrow_array) {
print OUTPUT $row[0],"\n";
}
$sth->finish();
}
$dbh->disconnect();
close(FILE) or die "Can't close commands.sql: $!";
close(OUTPUT) or die "Can't close output.txt: $!";
$
$ # run the perl program
$ perl test_dbi.pl
$
$ cat output.txt
20
30
30
20
30
30
10
20
$
$