在删除oracle过程中的行之前,创建与sysdate连接的备份表
我创建了一个包,并定义了一个过程来删除游标检索到的特定行。 在从表中删除行之前,我希望在每次编译包时备份这些记录,并且需要将备份表创建为与sysdate连接的tablename 例如:如果表名为emp,则应将备份表创建为emp_2020_10_16 下面是我创建的示例代码:在删除oracle过程中的行之前,创建与sysdate连接的备份表,oracle,plsql,Oracle,Plsql,我创建了一个包,并定义了一个过程来删除游标检索到的特定行。 在从表中删除行之前,我希望在每次编译包时备份这些记录,并且需要将备份表创建为与sysdate连接的tablename 例如:如果表名为emp,则应将备份表创建为emp_2020_10_16 下面是我创建的示例代码: PROCEDURE DELETE_REC( P_retcode NUMBER, P_errorbuff VARCHAR2, P_unit_id NUMBER, P_
PROCEDURE DELETE_REC(
P_retcode NUMBER,
P_errorbuff VARCHAR2,
P_unit_id NUMBER,
P_join_date VARCHAR2
)
IS
CURSOR cur1
IS
SELECT unit_ID,dept_ID,join_DATE
FROM EMP MMT
WHERE MMT.dep_TYPE_ID IN (44,35)
AND MMT.unit_id = P_unit_id
AND MMT.join_date < to_date(P_join_date,'RRRR/MM/DD HH24:MI:SS');
BEGIN
--begin
-- EXECUTE IMMEDIATE 'Create table EMP_' || to_char(sysdate,'yyyy_mm_dd') || ' as select * from EMP MMT WHERE MMT.dep_TYPE_ID IN (44,35)
AND MMT.unit_id = P_unit_id
AND MMT.join_date < to_date(P_join_date,'RRRR/MM/DD HH24:MI:SS');
--
-- end;
/*Here i would like to create backup table like above before executing the below delete statement but i am not sure about the correct standards that i should be using for above dynamic statement*/
FOR val IN cur1
LOOP
DELETE
FROM EMP MMT
WHERE MMT.dept_ID= val.dept_id;
如何以最佳方式使用上述动态语句备份表?我还在学习PL&SQL。也许这样的东西会有帮助:
create table employees as select * from hr.employees;
--drop table emp_2020_10_18;
--drop table employees;
----------------
declare
vTabName varchar2(50);
nDept_id number := 10;
nCnt number := 0;
vSQL varchar2(1000);
begin
vTabName := 'emp_'||to_char(sysdate, 'yyyy_mm_dd');
-- check if table exists
begin
execute immediate 'select count(*) from emp_tmp' into nCnt;
exception when others then
nCnt := -1;
end;
-- if not exists create one
if nCnt = -1 then
execute immediate 'create table '|| vTabName||' as select * from employees where 1=2' ;
end if;
execute immediate 'insert into '|| vTabName ||' select * from employees where department_id = :nDept_id' using nDept_id;
delete from employees where department_id = nDept_id;
exception when others then
dbms_output.put_line(sqlerrm);
end;
/