我在oracle中有两个存储过程。
第一个存储过程proc1
,返回表emp
中的一些行。
在另一个存储过程proc2
中,我需要调用proc1
并将其结果集插入另一个表中。
是否可以这样做或者还有其他更好的方法吗?
-- this is proce_2
create or replace PROCEDURE FIND_EMPLOYEES
(
IN_REGION_ID IN NUMBER,
<< more params >> ,
employee_cursor OUT SYS_REFCURSOR
)
IS
BEGIN
open employee_cursor for
select * from employee where <<some conditions>> ;
END;
答案 0 :(得分:2)
您可以将REF CURSOR用作常规explicit cursor:
SQL> CREATE TABLE employee AS
2 SELECT rownum id, 'XXX' name
3 FROM DUAL CONNECT BY level <= 10;
Table created.
SQL> CREATE OR REPLACE PROCEDURE find_employees (
2 emp_id NUMBER,
3 employee_cursor OUT SYS_REFCURSOR
4 ) IS
5 BEGIN
6 open employee_cursor for
7 select * from employee WHERE id = emp_id;
8 END;
9 /
Procedure created.
SQL> CREATE OR REPLACE PROCEDURE do_something IS
2 cc sys_refcursor;
3 l employee%rowtype;
4 BEGIN
5 find_employees(1, cc);
6 LOOP
7 FETCH cc INTO l;
8 EXIT WHEN cc%NOTFOUND;
9 dbms_output.put_line(l.id||'-'||l.name);
10 END LOOP;
11 END;
12 /
Procedure created.
SQL> EXEC do_something;
1-XXX
答案 1 :(得分:2)
以下是将一行从EMPLOYEES
复制到EMPLOYEES_COPY
的两个示例。
我希望它会对你有所帮助。
cursors
和packages
:DROP TABLE EMPLOYEES;
/
DROP TABLE EMPLOYEES_COPY;
/
CREATE TABLE EMPLOYEES
(
ID INT,
FIRST_NAME varchar(30),
LAST_NAME varchar(30)
);
insert into EMPLOYEES values(1,'AA','BBB');
insert into EMPLOYEES values(2,'CC','GGG');
insert into EMPLOYEES values(3,'EEE','MMM');
insert into EMPLOYEES values(4,'FFF','ZZZ');
/
CREATE TABLE EMPLOYEES_COPY
(
ID INT,
FIRST_NAME varchar(30),
LAST_NAME varchar(30)
);
/
CREATE OR REPLACE PACKAGE EMPLOYEE as
type t_cursor is ref cursor;
procedure FIND(id_emp int, curEMPLOYEE out t_cursor);
procedure INSERT_COPY(id_emp int);
end EMPLOYEE;
/
CREATE OR REPLACE PACKAGE BODY EMPLOYEE as
---------------------------------------------
procedure FIND(id_emp int,curEMPLOYEE out t_cursor) as
begin
open curEMPLOYEE for SELECT id, FIRST_NAME, LAST_NAME
FROM EMPLOYEES
WHERE id= id_emp;
end FIND;
---------------------------------------------
procedure INSERT_COPY(id_emp int) as
cur t_cursor;
emp employees%rowtype;
begin
FIND(id_emp=>id_emp,curEMPLOYEE=>cur);
LOOP
FETCH cur INTO emp;
EXIT WHEN cur%NOTFOUND;
insert into EMPLOYEES_COPY
values(emp.id,emp.FIRST_NAME,emp.LAST_NAME);
END LOOP;
end INSERT_COPY;
end EMPLOYEE;
/
包非常有用,因为您可以对单个实体的功能进行分组。在示例中,我将职能分组为员工。
更多信息
DROP TABLE tempEMPLOYEES;
/
DROP TABLE EMPLOYEES;
/
DROP TABLE EMPLOYEES_COPY;
/
CREATE TABLE EMPLOYEES
(
ID INT,
FIRST_NAME varchar(30),
LAST_NAME varchar(30)
);
insert into EMPLOYEES
values(1,'AA','BBB');
insert into EMPLOYEES
values(2,'CC','GGG');
insert into EMPLOYEES
values(3,'EEE','MMM');
insert into EMPLOYEES
values(4,'FFF','ZZZ');
/
CREATE TABLE EMPLOYEES_COPY
(
ID INT,
FIRST_NAME varchar(30),
LAST_NAME varchar(30)
);
/
CREATE GLOBAL TEMPORARY TABLE tempEMPLOYEES (
ID INT,
FIRST_NAME varchar(30),
LAST_NAME varchar(30)
) ON COMMIT DELETE ROWS;
/
CREATE OR REPLACE PACKAGE EMPLOYEE as
procedure INSERT_COPY(id_emp int);
end EMPLOYEE;
/
CREATE OR REPLACE PACKAGE BODY EMPLOYEE as
---------------------------------------------
procedure COPY_TO_TEMP(id_emp int) as
begin
insert into tempEMPLOYEES
SELECT id, FIRST_NAME, LAST_NAME
FROM EMPLOYEES
WHERE id= id_emp;
end COPY_TO_TEMP;
---------------------------------------------
procedure INSERT_COPY(id_emp int) as
begin
COPY_TO_TEMP(id_emp);
insert into EMPLOYEES_COPY
SELECT id, FIRST_NAME, LAST_NAME
FROM tempEMPLOYEES;
--WHERE id= id_emp; <---You can add this If you need.
end INSERT_COPY;
end EMPLOYEE;
/
Here是关于观点的更多信息。
您可以使用以下方法测试这两个示例:
DECLARE
ID_EMP NUMBER;
BEGIN
ID_EMP := 4;
EMPLOYEE.INSERT_COPY ( ID_EMP => ID_EMP) ;
commit;
END;