1

有人可以帮我告诉我如何在 Perl 脚本中调用 oracle 子例程

我在 oracle db 中已经存在一个过程。下面说

案例 1) 返回一个引用游标并接受一个 IN 参数。

CREATE OR REPLACE PROCEDURE procCursorExample(
  cursorParam OUT SYS_REFCURSOR, userNameParam IN VARCHAR2)
IS
BEGIN
  OPEN cursorParam FOR
    SELECT * FROM DBUSER WHERE USERNAME = userNameParam;
  END;

在 SQL 开发人员中,我可以直接执行它:

DECLARE
  dbUserCursor SYS_REFCURSOR;
  dbUserTable DBUSER%ROWTYPE;
BEGIN
  procCursorExample(dbUserCursor,'mkyong');
  LOOP
    FETCH dbUserCursor INTO dbUserTable;
    EXIT WHEN dbUserCursor%NOTFOUND;
    dbms_output.put_line(dbUserTable.user_id);
  END LOOP;
  CLOSE dbUserCursor;
END;

有人能告诉我如何通过 Perl 脚本调用带有参数的子例程吗

安瑟

#!/usr/bin/perl
use warnings ;
use strict ;
use DBI;
print "Connecting to DB..";
my $dbh = DBI->connect('dbi:Oracle:xe',  'scott', 'tiger') or
          die "Cannot connect to DB => " . DBI->errstr;

# prepare ????????

我不确定准备声明。任何帮助都是非常可观的。

4

1 回答 1

1

请阅读使用DBD::Oracle调用 Perl 存储过程的文档示例,这是您正在使用的驱动程序。

从这个链接具体

use DBI;
 
my($db, $csr, $ret_val);
 
$db = DBI->connect('dbi:Oracle:database','user','password')
      or die "Unable to connect: $DBI::errstr";
 
# So we don't have to check every DBI call we set RaiseError.
# See the DBI docs now if you're not familiar with RaiseError.
$db->{RaiseError} = 1;
 
# Example 1        Eric Bartley <bartley@cc.purdue.edu>
#
# Calling a PLSQL procedure that takes no parameters. This shows you the
# basic's of what you need to execute a PLSQL procedure. Just wrap your
# procedure call in a BEGIN END; block just like you'd do in SQL*Plus.
#
# p.s. If you've used SQL*Plus's exec command all it does is wrap the
#      command in a BEGIN END; block for you.
 
$csr = $db->prepare(q{
  BEGIN
    PLSQL_EXAMPLE.PROC_NP;
  END;
});
$csr->execute;
 
 
# Example 2        Eric Bartley <bartley@cc.purdue.edu>
#
# Now we call a procedure that has 1 IN parameter. Here we use bind_param
# to bind out parameter to the prepared statement just like you might
# do for an INSERT, UPDATE, DELETE, or SELECT statement.
#
# I could have used positional placeholders (e.g. :1, :2, etc.) or
# ODBC style placeholders (e.g. ?), but I prefer Oracle's named
# placeholders (but few DBI drivers support them so they're not portable).
 
my $err_code = -20001;
 
$csr = $db->prepare(q{
        BEGIN
            PLSQL_EXAMPLE.PROC_IN(:err_code);
        END;
});
 
$csr->bind_param(":err_code", $err_code);
 
# PROC_IN will RAISE_APPLICATION_ERROR which will cause the execute to 'fail'.
# Because we set RaiseError, the DBI will croak (die) so we catch that with eval.
eval {
  $csr->execute;
};
print 'After proc_in: $@=',"'$@', errstr=$DBI::errstr, ret_val=$ret_val\n";
 
 
# Example 3        Eric Bartley <bartley@cc.purdue.edu>
#
# Building on the last example, I've added 1 IN OUT parameter. We still
# use a placeholders in the call to prepare, the difference is that
# we now call bind_param_inout to bind the value to the place holder.
#
# Note that the third parameter to bind_param_inout is the maximum size
# of the variable. You normally make this slightly larger than necessary.
# But note that the Perl variable will have that much memory assigned to
# it even if the actual value returned is shorter.
 
my $test_num = 5;
my $is_odd;
 
$csr = $db->prepare(q{
        BEGIN
            PLSQL_EXAMPLE.PROC_IN_INOUT(:test_num, :is_odd);
        END;
});
 
# The value of $test_num is _copied_ here
$csr->bind_param(":test_num", $test_num);
 
$csr->bind_param_inout(":is_odd", \$is_odd, 1);
 
# The execute will automagically update the value of $is_odd
$csr->execute;
 
print "$test_num is ", ($is_odd) ? "odd - ok" : "even - error!", "\n";
 
 
# Example 4        Eric Bartley <bartley@cc.purdue.edu>
#
# What about the return value of a PLSQL function? Well treat it the same
# as you would a call to a function from SQL*Plus. We add a placeholder
# for the return value and bind it with a call to bind_param_inout so
# we can access its value after execute.
 
my $whoami = "";
 
$csr = $db->prepare(q{
        BEGIN
            :whoami := PLSQL_EXAMPLE.FUNC_NP;
        END;
});
 
$csr->bind_param_inout(":whoami", \$whoami, 20);
$csr->execute;
print "Your database user name is $whoami\n";
 
$db->disconnect;
于 2020-08-23T06:23:33.530 回答