Running a PL/SQL procedure in a Perl script

倾然丶 夕夏残阳落幕 提交于 2019-12-23 00:28:09

问题


I have a Perl script which takes a file as input and has PL/SQL (for statements and DBMS_OUTPUT.PUT_LINE) in it. I need to run make a database connection and run that file in Perl script.The pl/sql has Begin declare end section,with for statements on it which is writing data of 3 columns separated using commas(DBMS_OUTPUT.PUT_LINE) I have shown what I have tried below. Is it correct?

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";
}

回答1:


$sth->fetch(), $sth->fetchrow_*(), and friends all fetch records from a result set. In Oracle PL/SQL blocks don't normally return result sets. So calling $sth->fetchrow_array() after running a PL/SQL block won't return any results.

If your using DBMS_OUTPUT.PUT_LINE to output results you will need to use the dbms_output_* functions provided by 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' );


来源:https://stackoverflow.com/questions/11612713/running-a-pl-sql-procedure-in-a-perl-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!