Need help in printing a sql query in perl

Hi All,

I have the following sql query

select abcd from udbadm.log where xyz='1'.

I have 16k queries similar to this with different values for xyz.

I want to print the values of 'abcd' for each row.

I have the following perl code, but not sure how i can print that particular column

open(OUTPUT, ">/tmp/output.txt") || die ("Can't open\n");
open (FILE, "/tmp/commands.sql") || die ("Can't open commands.sql\n");
while (<FILE>)
{
$sql = $;
chomp $sql;
$update = $dbh->do($sql);
if($update)
{
print OUTPUT "Success:Now executing $sql\n"; - Here i want to print 'abcd' instead of the query for each run
}
else
{
print OUTPUT "Failure:";
$update = null;
$sql=$
;
$update = $dbh->do($sql);
print OUTPUT "Now executing $sql\n";
}
}

Appreciate your help. Thanks in advance

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

Hope that helps,
tyler_durden