oracle 的备份语句怎么写?

qingfengleng 2002-10-25 09:52:27
各位大虾,我在写一个存储过程来备份一个用户的所有数据记录,但没有手册,不知该该怎么写?我想按用户来备份数据,(最好就像导入导出用户数据那样的)还有恢复语句怎么写?
...全文
328 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
chooser 2002-10-25
  • 打赏
  • 举报
回复
create or replace procedure backup_table_to_file /*功能:将ORACLE的任意表数据导出(标准格式) */
(
v_tablename varchar2, /*需要导出的表名*/
v_path varchar2, /*服务器上导出数据文件的目录*/
v_string varchar2, /*服务器上导出数据文件的文件名*/
v_where varchar2, /*查询条件*/
v_flag varchar2, /*写数据文件的方式 w:重写 a:追加*/
outflag out varchar2 /*返回处理结果 0 成果、1 写入目录有误、2 表名有误、3 写入目录不能为空、4 写入文件方式有误、5 查询条件有误、6 其他错误*/
)
is
file_handle utl_file.file_type;
path_notinput_exception EXCEPTION;
table_notfind_exception EXCEPTION;
write_type_exception EXCEPTION;
type ref_cursor_type is REF CURSOR;
cursor_select ref_cursor_type;
outputline varchar2(1000) ;
select_cname varchar2(1000) ;
w_flag varchar2(10);
get_cname varchar2(1000) ;
put_cname varchar2(1000) ;
temp varchar2(1000) ;
resault varchar2(1000) ;
filepath varchar2(100) ;
filename varchar2(100) ;
i integer;
begin
outflag :='0';
IF (v_path is null) THEN --初始化服务器文件夹
RAISE path_notinput_exception;
ELSE
filepath := v_path;
END IF; --初始化服务器文件夹
get_cname := '';
temp := nls_upper(v_tablename);
if (v_flag is null) then --初始化写文件方式
w_flag := 'w';
else
w_flag := v_flag;
end if; --初始化写文件方式
if w_flag in ('W','w','A','a') then
select_cname := 'select cname from col where tname = '||''''||temp||'''';
OPEN cursor_select for select_cname;
FETCH cursor_select into get_cname;
IF (get_cname is null) THEN
RAISE table_notfind_exception;
END IF;
put_cname := ''''||'"'||''''||'||'||get_cname||'||'||''''||'"'||'''';
WHILE cursor_select%FOUND LOOP
FETCH cursor_select into get_cname;
EXIT WHEN cursor_select%NOTFOUND;
put_cname :=put_cname ||'||'||''''||','||''''||'||'||''''||'"'||''''||'||'||get_cname||'||'||''''||'"'||'''';
END LOOP;
CLOSE cursor_select;
IF (v_string is null) then
select_cname := 'select to_char(sysdate,'||''''||'yyyymmdd'||''''||') from DUAL';
OPEN cursor_select for select_cname;
FETCH cursor_select into filename;
CLOSE cursor_select;
filename := v_tablename||'.'||filename||'.txt';
ELSE
filename := v_string;
END IF;
file_handle :=utl_file.fopen(filepath,filename,w_flag);
select_cname := 'select '||put_cname||' from '||temp||' '||v_where;
resault := '';
OPEN cursor_select for select_cname;
FETCH cursor_select into resault;
WHILE cursor_select%FOUND LOOP
outputline := resault;
utl_file.put_line(file_handle,outputline);
FETCH cursor_select into resault;
END LOOP;
CLOSE cursor_select;
utl_file.fclose(file_handle);
ELSE
RAISE write_type_exception;
end if;
exception
when utl_file.invalid_path then
outflag :='1';
--raise_application_error(-20001,'错误:主机的写入文件目录有误!');
when table_notfind_exception then
outflag :='2';
--raise_application_error(-20002,'错误:输入的表名有误!');
when path_notinput_exception then
outflag :='3';
--raise_application_error(-20003,'错误:服务器的写入文件目录为必输项!');
when write_type_exception then
outflag :='4';
--raise_application_error(-20004,'错误:写入文件方式有误!');
when others then
if sqlcode like '-9%' then
outflag :='5';
--raise_application_error(-20005,'错误:查询条件有误!');
else
outflag :='6';
raise_application_error(sqlcode,sqlerrm);
end if;
end backup_table_to_file;
然后用select unique tname from col;可以取得该用户所有表名,知道怎么写了吧^_^
wv2000 2002-10-25
  • 打赏
  • 举报
回复
利用exp help=y 可以查看EXP的联机帮助信息
按用户备份,可以这样:
exp system/manager@service name file=xxxx.dmp owner=(user1,user2,user3....)

利用imp help=y 可以查看IMP的联机帮助信息
imp system.manager@service name fromuser=system touser=(user1,user2,user3....) file=xxxx.dmp
qingfengleng 2002-10-25
  • 打赏
  • 举报
回复
谢谢

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧