怎么用ORACLE PLSQL匿名块调用存储过程
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么用ORACLE PLSQL匿名块调用存储过程相关的知识,希望对你有一定的参考价值。
现在有一个存储过程
producre core_acct_compare(p_branch in varchar2,
p_format in varchar2,
p_collate_date in varchar2,
p_status in varchar2)
is
v_prior_date varchar(8 char);
v_start_date varchar(8 char);
v_invalid exception;
BEGIN
...具体过程...
END core_acct_compare;
怎么用匿名块调用一下这个存储过程?
core_acct_compare(参数1,参数2,参数3,参数4);
end;
begin ... end; 就是所谓的匿名块
本回答被提问者和网友采纳从匿名块调用存储过程
【中文标题】从匿名块调用存储过程【英文标题】:calling stored procedure from anonymous block 【发布时间】:2014-06-01 07:26:14 【问题描述】:我在本地机器上的 Oracle 11gr2 数据库上用 sql developer 编写的 plsql 存储过程读取结果时遇到问题。
这是我的桌子:
create table MY_TEST_TABLE
(employee_id NUMBER(6)
,first_name VARCHAR2(20)
,last_name VARCHAR2(25)
,email VARCHAR2(25)
,phone_number VARCHAR2(20));
这是过程声明:
create or replace PACKAGE TEST_PACKAGE AS
procedure test_procedure (i_id in number,
o_data out sys_refcursor);
END TEST_PACKAGE;
这是身体:
create or replace PACKAGE BODY TEST_PACKAGE AS
procedure test_procedure (i_id in number,
o_data out sys_refcursor) AS
BEGIN
open o_data for
select employee_id,
first_name,
last_name,
email,
phone_number
from my_test_table
where EMPLOYEE_ID = i_id;
close o_data;
END test_procedure;
END TEST_PACKAGE;
这是匿名块调用:
SET serveroutput on
DECLARE
in_id number;
my_cursor sys_refcursor;
current_record my_test_table%ROWTYPE;
BEGIN
in_id := 1;
test_package.test_procedure(in_id, my_cursor);
open my_cursor;
LOOP
FETCH my_cursor INTO current_record;
EXIT WHEN my_cursor%NOTFOUND;
dbms_output.put_line(' - out - ' || current_record.employee_id);
END LOOP;
END;
我收到错误:
Error starting at line : 2 in command -
DECLARE
in_id number;
my_cursor sys_refcursor;
current_record my_test_table%ROWTYPE;
BEGIN
in_id := 1;
test_package.test_procedure(in_id, my_cursor);
open my_cursor;
LOOP
FETCH my_cursor INTO current_record;
EXIT WHEN my_cursor%NOTFOUND;
dbms_output.put_line(' - out - ' || current_record.employee_id);
END LOOP;
END;
Error report -
ORA-06550: line 8, column 5:
PLS-00382: expression is of wrong type
ORA-06550: line 8, column 5:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
有人可以解释什么是错的吗? 天呐!
【问题讨论】:
【参考方案1】:游标在过程中打开,因此您不需要也不能直接在匿名块中打开它。嗯,它应该是打开的,但你也在程序中关闭它。从过程中删除close
,从块中删除open
:
create or replace PACKAGE BODY TEST_PACKAGE AS
procedure test_procedure (i_id in number,
o_data out sys_refcursor) AS
BEGIN
open o_data for
select employee_id,
first_name,
last_name,
email,
phone_number
from my_test_table
where EMPLOYEE_ID = i_id;
-- close o_data;
END test_procedure;
END TEST_PACKAGE;
/
还有:
DECLARE
in_id number;
my_cursor sys_refcursor;
current_record my_test_table%ROWTYPE;
BEGIN
in_id := 1;
test_package.test_procedure(in_id, my_cursor);
-- open my_cursor;
LOOP
FETCH my_cursor INTO current_record;
EXIT WHEN my_cursor%NOTFOUND;
dbms_output.put_line(' - out - ' || current_record.employee_id);
END LOOP;
END;
/
SQL Fiddle - 只需添加数据...
【讨论】:
以上是关于怎么用ORACLE PLSQL匿名块调用存储过程的主要内容,如果未能解决你的问题,请参考以下文章