Archive

Archive for the ‘Perl Scripts’ Category

Sample Perl Script to connect Oracle DB

February 25, 2011 Leave a comment

#!/usr/bin/perl
# Run under Oracle Perl, for DBI.
BEGIN {
die “ORACLE_HOME not set\n” unless $ENV{ORACLE_HOME};
unless ($ENV{OrAcLePeRl}) {
$ENV{OrAcLePeRl} = “$ENV{ORACLE_HOME}/perl”;
$ENV{PERL5LIB} = “$ENV{PERL5LIB}:$ENV{OrAcLePeRl}/lib:$ENV{OrAcLePeRl}/lib/site_perl”;
$ENV{LD_LIBRARY_PATH} = “$ENV{LD_LIBRARY_PATH}:$ENV{ORACLE_HOME}/lib32:$ENV{ORACLE_HOME}/lib”;
exec “$ENV{OrAcLePeRl}/bin/perl”, $0, @ARGV;
}
}

use strict;
use warnings;

use DBI;
my (@Table);
my($username, $password) = (‘vi’, ‘vi’);

my $dbh = DBI->connect(“dbi:Oracle:$ENV{ORACLE_SID}”, $username, $password) or die;
my $sql = qq{ SELECT tname FROM tab where rownum < 10};
my $sth = $dbh->prepare( $sql );
$sth->execute();
while ( @Table = $sth->fetchrow_array(  ) ) {
print “Table Name: @Table\n”;
}
[oracle@rh55-64b ~]$

Reference : http://www.orafaq.com/wiki/Perl

Categories: Perl Scripts