How to execute a Pl sql file from a perl script

2019-09-19 04:23发布

Hi folks can you please help me in understanding how to call a pl sql file from perl script

I have a pl sql file like this

DECLARE
 x NUMBER := 100;
BEGIN
FOR i IN 1..10 LOOP
  IF MOD(i,2) = 0 THEN     -- i is even
     INSERT INTO temp VALUES (i, x, 'i is even');
  ELSE
     INSERT INTO temp VALUES (i, x, 'i is odd');
  END IF;
  x := x + 100;
END LOOP;
COMMIT;
END; 

The file is named test.sql I want to call this file from a perl script. I know first we have to connect to db and then perform the process but I Don now know how to execute this file from a perl script

标签: sql oracle perl
1条回答
Emotional °昔
2楼-- · 2019-09-19 05:10

Basically you need to

  • use the DBI module with the appropriate driver (Oracle or whatever)
  • slurp in the script into a variable by using plain perl
  • open a DB connection
  • prepare the slurped in script
  • execute the statement handle
  • disconnect from the DB

Here is an example (I am not showing how to slurp in the script):

use DBI;
use DBD::Oracle;

my $service="xxx";
my $user = "yyy";
my $pass = "zzz";

my $DBH = DBI->connect
  (
   "dbi:Oracle:$service", 
   "$user", "$pass",
   { 
    RaiseError => 0, 
    PrintError => 0, 
    AutoCommit => 0, 
    ShowErrorStatement  => 0
   }
  ) or die;

my $script = qq(
    declare
        x number := 1;
    begin
        insert into xxx values (x);
        commit;
    end;
);

my $sth = $DBH->prepare($script) or die;
$sth->execute() or die;

$DBH->disconnect();
查看更多
登录 后发表回答