0

I am new to Perl and am trying to create a script that connects to my Oracle SQL Server database and returns the results of a table query via email. I am having trouble with the database connection part. Any ideas where I should start? Any example code would be greatly appreciated. Thanks,

6
  • 3
    try DBI you can find an example here Commented Aug 9, 2017 at 20:29
  • 3
    SQL Server is the brand name of Microsoft's RDBMS, not Oracle's. Commented Aug 9, 2017 at 20:45
  • In my experience, the hardest part of this by far is the non-perl part: installing the Oracle client library. I had to tell the library how to connect using an obscure configuration file and reference that configuration in the DBI connect string, instead of passing the hostname and port directly from the perl script like I wanted to. DBD::Oracle was so hard to build I ended up routing the thing through DBD::ODBC instead (which involves even more configuration far removed from the perl script). Oh how I wished for DBD::Oracle::PurePerl Commented Aug 10, 2017 at 1:49
  • 1
    Oracle and Microsoft SQL Server are two different database products. Which one are you using? Commented Aug 10, 2017 at 3:23
  • 1
    Just out of curiosity, what OS are you trying to connect from? Commented Aug 11, 2017 at 21:00

3 Answers 3

1

Below is some sample code to get you connected to Oracle using the Perl DBI module. Adjust database name, username, and password as necessary.

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

my $dbName = 'mydb';
my $username = 'username';
my $password = 'password';
my $options =  { RaiseError => 1 };
my $dbh = DBI->connect("dbi:Oracle:${dbName}", $username, $password, $options);

$dbh is a database handle that you can use to execute all the queries that you like. See the DBI documentation page at CPAN for a concise description of the methods available.

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

3 Comments

Thanks Kirby. I am getting message when I run this perl script from the command-line "Can't locate object method "connect" via package "DBI" (perhaps you forgot to load "DBI"?) at main.pl line 8." Any idea how to resolve? thanks
I neglected to include the use DBI; line. Please see the updated answer. Also bear in mind that my answer assumes that you already have installed Oracle client libraries and Perl module DBD::Oracle.
no i didn't download any of that stuff... how do i do that? thanks
0

First you should be aware of TNSNAMES.ORA file, which has predefined connections in form of

ORA11 =
 (DESCRIPTION = 
   (ADDRESS_LIST =
     (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.0)(PORT = 1521))
   )
 (CONNECT_DATA =
   (SERVICE_NAME = ORA12)
 )
)

(check for above connection on server host if you happen to be on different machine)

Now you can use ORA11 as db name

  my $DB = DBI->connect(
    "dbi:Oracle:",
    "USER/PASSWORD\@ORA11",
    "",
    {
      # ChopBlanks => 1,
      # AutoCommit => 0,
    },
  );

or use complete connection string instead of ORA11:

"USER/PASSWORD\@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.0)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=ORA12)))"

More connection options can be found in DBD::Oracle

Comments

0

I have a few dozen scripts that perform Oracle, Sybase, and MS SQL queries; even some scripts that perform queries on multiple databases and combine results, or build a query for one database based on result of a prior query ... but after many days of trying to get Perl libraries to play well.

For those of us that are real hacks, we start by using the Oracle SQL command-line client sqlplus.exe, which makes this approach easy, but far from pretty:

my $run_sql = 'sqlplus.exe -s DBuser/DBpwd@DBname < SQLfile.sql';
my $SQLfile = "temp.sql"; 

sub GET_EMP_LIST
{
  my $status = $_[0];
  my $sql_text = "
  set linesize 150
  set pagesize 0
  set numf 99999999999
  set feedback off
  SELECT  
    EMP.FIRST          || ',' ||
    EMP.LAST           || ',' ||
    EMP.PHONE          || ',' ||
    EMP.SALARY
  FROM 
    PERSONNEL.EMPLOYEES  EMP
  WHERE
    (EMP.STATUS = '$status')
  \;";

  open (SQL, $FileOpenWrite, "$SQLfile");
  print SQL $sql_text;
  close (SQL_TEXT);
  my @Results = "$run_SQL";
  unlink $SQLfile;
  return @Results;
}

#MAIN

@Employees = GET_EMP_LIST "Active";
for (@Employees)
{
  my $temp = chomp $_;
  $temp =~ s/\s+//g;        #get rid of white spaces
  my ($FIRST, $LAST, $PHONE, $SALARY) = split /,/, $temp;

  .... do something with it ....
}

Like I say, far from pretty, but quick and easy, and using SQL query tools, like TOAD, you can generate the SQL in a drag & drop program before you integrate into your script.

I know many folks will say this is a terrible solution, but we pull in data that includes hundreds of thousands of lines of data.

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.