下面有两个示例将一行从复制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;
/
这是有关视图的更多信息。
您可以使用以下方法测试这两个示例:
DECLARE
ID_EMP NUMBER;
BEGIN
ID_EMP := 4;
EMPLOYEE.INSERT_COPY ( ID_EMP => ID_EMP) ;
commit;
END;