2

我有一个 Perl 脚本,它接受一个文件作为输入,并在其中包含 PL/SQL(用于语句和DBMS_OUTPUT.PUT_LINE)。我需要运行建立一个数据库连接并在 Perl 脚本中运行该文件。pl/sql 有 Begin declare end 部分,上面有 for 语句,它正在写入使用逗号分隔的 3 列的数据(DBMS_OUTPUT.PUT_LINE)我已经展示了什么我在下面尝试过。这是对的吗?

my $dbh = DBI->connect( "dbi:Oracle:$db", $username, $passwd ) ||
        die( $DBI::errstr . "\n" );
$dbh->{AutoCommit} = 0;
$dbh->{PrintError} = 1;

open FILE, $sql_file or die "Could not open file";

$query = '';
while($line = <FILE>) {
    chomp($line);
    $query .= $line;
}

my $sth = $dbh->prepare($query);

$sth->execute();

while ( my @row = $sth->fetchrow_array() ) {
    foreach (@row) {
        print "$_";
    }
    print "\n";
}
4

1 回答 1

4

$sth->fetch(), $sth->fetchrow_*(), 和朋友都从结果集中获取记录。在 Oracle PL/SQL 中,块通常不返回结果集。因此,在运行 PL/SQL 块后调用$sth->fetchrow_array()不会返回任何结果。

如果您使用DBMS_OUTPUT.PUT_LINE输出结果,您将需要使用dbms_output_*DBD::Oracle 提供的函数

my $dbms_output_byte_limit = 1000000;
$dbh->func( $dbms_output_byte_limit, 'dbms_output_enable' );

my $sth = $dbh->prepare($query);
$sth->execute();

my @text = $dbh->func( 'dbms_output_get' );
于 2012-07-23T16:02:44.977 回答