Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7813153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:45:13+00:00 2026-06-02T04:45:13+00:00

I have a Perl script that uses a DBI connection. I open and read

  • 0

I have a Perl script that uses a DBI connection. I open and read the SQL script file using a sub routine. I am printing only one record, where I should have two more (three records total). How do I get all the records?

Result:

Alert:OUTBOUND_DATA:0

Script:

my $dbh_oracle = DBI->connect(
          $CFG{oracle_dbi_connect},
          $CFG{db_user},
          $CFG{db_cred},
          {AutoCommit => 0,
           RaiseError => 0,
           PrintError => 0}) or die ("Cannot connect to the database: ".$DBI::errstr."\n");

my ($val1, $val2) = get_data();
print "Alert:$val1:$val2\n";

send_email("Alert:$val1:$val2");

sub get_data
{
  undef $/;
  open (my $QFH, "< /sql/summary.sql") or die "error can't open this file $!";
  my $sth= $dbh_oracle->prepare(<$QFH>) or
      die ("Cannot connect to the database: ".$DBI::errstr."\n");
  $sth->execute;
  close $QFH;
  my $row = $sth->fetchrow_hashref;
  $sth->finish;
  return @$row{'MYTABLE','FLAG'};
}

sub send_email {
    my $message = shift;
    open (MAIL, "|/usr/sbin/sendmail -t") or die "Can't open sendmail: $!";
    print MAIL "To: me\@test.com\n";
    print MAIL "From: Data\n";
    print MAIL "\n";
    print MAIL $message;
    close MAIL;
}
exit;

RESULTS from running query: (more than 1 rec)

MYTABLE                  FLAG
----------------------- ----------
OUTBOUND_DATA         0
MSGS_BY_DIM                  0
INBOUND_DATA         0

3 rows selected.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:45:16+00:00Added an answer on June 2, 2026 at 4:45 am

    It also depends on how you are structuring your overall script. Your get_data() call only allows a single pair of values to be returned. I see at least a couple options: either return a hash (reference) containing all the data and let the main assemble it, or use the loop constructs mentioned previously and fabricate the message body inside the subroutine, returning only a single scalar string.

    To return all the data as a hash reference, the get_data subroutine might look like this (note I’m using fetchall_hashref instead of fetchrow_hashref:

    sub get_data
    {
      undef $/;
      open (my $QFH, "< /sql/summary.sql") or die "error can't open this file $!";
      my $sth= $dbh_oracle->prepare(<$QFH>) or
          die ("Cannot connect to the database: ".$DBI::errstr."\n");
      $sth->execute;
      close $QFH;
      my $hash_ref = $sth->fetchall_hashref('MYTABLE');
      $sth->finish;
      return $hash_ref;
    }
    

    And you call it from main and use the output as follows:

    my $hash_ref = get_data();
    my $message = "";
    foreach my $table (sort keys %$hash_ref) {
        $message .= join(":", "Alert", $table, $$hash_ref{$table}{'FLAG'}) . "\n";
    }
    

    This will result in $message containing:

    Alert:INBOUND_DATA:0
    Alert:MSGS_BY_DIM:0
    Alert:OUTBOUND_DATA:0
    

    And you may want to politely:

    $dbh_oracle->disconnect;
    

    before you exit.

    This has some problems, for example you’ve got the SQL stashed in an external script, but I have resorted to hard-coding the key (MYTABLE, which I am presuming is unique in your query) and the value (FLAG) in the script, which will be limiting later when you want to expand on this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Perl script that uses WWW::Mechanize to read from a file and
I have a Perl script that works on Windows XP. It uses File::Copy's move
I have a Perl script that processes a bunch of file names, and uses
I have a Perl script that I'm attempting to set up using Perl Threads
I have a perl script that's reading an INI file like this: [placeholder_title] Hostname
I currently have a Perl CGI script that parses incoming XML requests using XML::Simple
I'd like a simple perl script / sub to read a sql server table
I have a Perl script that uses Term::ANSIColor . It used to be the
I have a perl script that uses the CGI::Session::Drive::memcached, but I want to be
I have a Perl script that uses Selenium to fetch a HTML document called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.