码迷,mamicode.com
首页 > 其他好文 > 详细

备份数据表、还原数据表

时间:2015-06-16 14:37:14      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

CREATE OR REPLACE PROCEDURE backuporg
IS
-- declare fixed array
TYPE arry_var IS VARRAY(30) OF VARCHAR2(32);
tblnames arry_var;
v_sql varchar2(3999);
tblname varchar2(32);
tblexist number;
--set serveroutput on size 10000000000;
BEGIN

-- init array
tblnames := arry_var(
‘org_department‘,
‘org_user‘,
‘org_group‘,
‘org_duty‘,
‘org_job‘,
‘org_department_principal‘,
‘org_dept_principal‘,
‘org_department_role‘,
‘org_department_user_job‘,
‘org_user_job‘,
‘org_user_leader‘,
‘org_user_privilege‘,
‘org_user_role‘,
‘org_job_leader‘,
‘org_user_property‘,
‘org_user_login_log‘,
‘org_job_function‘,
‘org_group_nested_group‘,
‘org_employee‘,
‘org_employee_job‘,
‘org_duty_agent‘,
‘org_department_function‘,
‘org_department_employee_job‘,
‘org_group_user‘,
‘org_group_member‘,
‘org_group_job‘,
‘org_group_duty‘,
‘org_group_department‘,
‘org_duty_role‘,
‘org_job_role‘);

--dbms_output.put_line(tblname(1));
--dbms_output.put_line(tblname(2));


for i in 1..tblnames.count
loop
tblname := tblnames(i)||‘_x‘;
select count(1) into tblexist from all_tables where TABLE_NAME = upper(tblname) ;

if tblexist=1 then
v_sql:= ‘ drop table ‘||tblname;
dbms_output.put_line(v_sql);
execute immediate (v_sql);
end if;

if tblexist=0 then

dbms_output.put_line(tblname ||‘ 不存在‘);

end if;


v_sql:= ‘ create table ‘||tblname||‘ as select * from ‘||tblnames(i);
dbms_output.put_line(‘创建表‘||v_sql);
execute immediate (v_sql);

-- || ‘ create table ‘||tblname(i)||‘_bak as select * from ‘||tblname(i)||‘;‘;
end loop;

 

 

 


END backuporg;

/*
begin
backuporg();
end;

*/

 

 

CREATE OR REPLACE PROCEDURE restoreorg
IS
-- declare fixed array
TYPE arry_var IS VARRAY(30) OF VARCHAR2(32);
tblnames arry_var;
v_sql varchar2(3999);
tblname varchar2(32);
baktblname varchar2(32);
tblexist number;
BEGIN

-- init array
tblnames := arry_var(
‘org_department‘,
‘org_user‘,
‘org_group‘,
‘org_duty‘,
‘org_job‘,
‘org_department_principal‘,
‘org_dept_principal‘,
‘org_department_role‘,
‘org_department_user_job‘,
‘org_user_job‘,
‘org_user_leader‘,
‘org_user_privilege‘,
‘org_user_role‘,
‘org_job_leader‘,
‘org_user_property‘,
‘org_user_login_log‘,
‘org_job_function‘,
‘org_group_nested_group‘,
‘org_employee‘,
‘org_employee_job‘,
‘org_duty_agent‘,
‘org_department_function‘,
‘org_department_employee_job‘,
‘org_group_user‘,
‘org_group_member‘,
‘org_group_job‘,
‘org_group_duty‘,
‘org_group_department‘,
‘org_duty_role‘,
‘org_job_role‘);

--dbms_output.put_line(tblname(1));
--dbms_output.put_line(tblname(2));

for i in 1..tblnames.count

loop
tblname := tblnames(i)||‘_y‘;
select count(1) into tblexist from all_tables where TABLE_NAME = upper(tblname) ;

if tblexist=1 then
v_sql:= ‘ drop table ‘||tblname;--删除掉备份表
dbms_output.put_line(v_sql);
execute immediate (v_sql);
end if;

if tblexist=0 then

dbms_output.put_line(tblname ||‘ 不存在‘);

end if;


-- alter table
-- ORG_DEPT_PRINCIPAL_bak
--rename to
-- ORG_DEPT_PRINCIPAL_JOB

--将正式表重重名为带y备份表
v_sql:= ‘ alter table ‘||tblnames(i)||‘ rename to ‘|| tblname ;--将原有表备份起来
dbms_output.put_line(‘将正式表重名为带y备份表‘||v_sql);
execute immediate (v_sql);


baktblname := tblnames(i)||‘_x‘; --备份表
v_sql:= ‘ alter table ‘||baktblname||‘ rename to ‘|| tblnames(i);--将备份表还原
dbms_output.put_line(‘将备份表还原‘||v_sql);
execute immediate (v_sql);

end loop;

END restoreorg;

/*
begin
backuporg();
end;

*/

备份数据表、还原数据表

标签:

原文地址:http://www.cnblogs.com/silentjesse/p/4580564.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!