【问题标题】:How to execute a Pl sql file from a perl script如何从 perl 脚本执行 Pl sql 文件
【发布时间】:2014-10-28 00:59:53
【问题描述】:

大家好,请帮助我理解如何从 perl 脚本调用 pl sql 文件

我有一个这样的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; 

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

【问题讨论】:

标签: sql oracle perl


【解决方案1】:

基本上你需要

  • 将 DBI 模块与适当的驱动程序(Oracle 或其他)一起使用
  • 使用普通 perl 将脚本插入变量中
  • 打开数据库连接
  • 准备 slurped in 脚本
  • 执行语句句柄
  • 断开与数据库的连接

这是一个例子(我没有展示如何在脚本中啜饮):

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();

【讨论】:

  • 嗨,马丁,啜饮的意思是我没听懂你
  • 我的意思是您创建了一个字符串变量(在我的示例中为 $script),其值是您希望在数据库中执行的整个脚本。谷歌为它。类似于 open FILE, ";打印“@lines\n”;你当然不需要打印它。
  • 您好,非常抱歉,我对 perl 完全陌生,正如您所说,我发现了一些东西,但您能帮我整合两者吗?
  • 你应该可以自己解决这个问题。
  • @user2647888 - 你让我看这个,但我看到你接受了这个答案。你还有问题吗?
猜你喜欢
  • 1970-01-01
  • 2013-04-08
  • 1970-01-01
  • 2021-02-15
  • 1970-01-01
  • 2018-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多