简体   繁体   English

如何从Perl脚本执行PL SQL文件

[英]How to execute a Pl sql file from a perl script

Hi folks can you please help me in understanding how to call a pl sql file from perl script 嗨,大家好,请您帮我了解如何从Perl脚本中调用pl sql文件

I have a pl sql file like this 我有一个像这样的pl sql文件

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. 该文件名为test.sql,我想从perl脚本中调用此文件。 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 我知道首先我们必须连接到db,然后执行该过程,但是我现在不知道如何从perl脚本执行此文件

Basically you need to 基本上你需要

  • use the DBI module with the appropriate driver (Oracle or whatever) 将DBI模块与相应的驱动程序(Oracle或其他任何驱动程序)一起使用
  • slurp in the script into a variable by using plain perl 通过使用纯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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM