当前位置:数据库 > Oracle >>

Oracle批量导出存储过程

Oracle批量导出存储过程
 
法一:
PL/SQL工具导出
 
法二:
Sql代码  
SET echo off;  
SET heading   off;  
SET feedback   off;  
spool C:\Documents and Settings\Administrator\桌面\proc.sql;  
 
--1、用sys用户等陆的话:
Sql代码  
select   text   from   dba_source   where   owner= 'lingfeng'    and   type   = 'PROCEDURE';  
 
--2、用一般用户(要导出其下存储过程的用户):
Sql代码  
select   text   from   user_source;  
spool   off;  
 
法三:(自己写存储过程导出)
1.创建DIRECTORY
Sql代码  
create or replace directory PROCE_DIR as 'C:/Documents and Settings/Administrator/桌面';  
 
如创建报没权限的错误,则以system的用户登录,给当前用户赋权
--赋权语句
Sql代码  
grant create any directory to bijian;  
 
--撤权语句
Sql代码  
revoke create any directory from bijian;  
 
创建之后可通过如下语句查询是否创建成功
Sql代码  
select * from dba_directories;  
 
2.创建导出存储过程
Sql代码  
create or replace procedure loadProce(owner varchar2)  
is  
    type user_source_table_type is table of user_source.text%TYPE INDEX BY BINARY_INTEGER;  
    user_source_table user_source_table_type;  
    file_handle utl_file.file_type;  
    stor_text VARCHAR2(4000);  
    sql_stat varchar2(1000);  
    sql_stat2 varchar2(1000);  
    sql_stat3 varchar2(1000);  
    nCount NUMBER;  
   i NUMBER;  
begin  
    sql_stat:='select distinct(name) from all_source where owner = ''' || upper(owner) || '''';  
    execute immediate sql_stat bulk collect into user_source_table;  
    file_handle:=utl_file.fopen('PROCE_DIR','test.sql','W');  
    for j in 1..user_source_table.count loop  
        i:=1;  
        sql_stat2:='SELECT MAX(LINE) FROM ALL_SOURCE WHERE OWNER=''' || upper(owner) || ''' AND NAME=''' || user_source_table(j) || '''';  
        --dbms_output.put_line(sql_stat2);  
        execute immediate sql_stat2 into nCount;  
        WHILE i<=nCount LOOP  
           sql_stat3:='SELECT TEXT FROM ALL_SOURCE WHERE OWNER=''' || upper(owner) || ''' AND NAME=''' || user_source_table(j) || ''' and line = ' || i;  
           --dbms_output.put_line(sql_stat3);  
           execute immediate sql_stat3 into stor_text;  
           i:=i+1;  
         utl_file.put(file_handle,stor_text);  
        END LOOP;  
    end loop;  
    utl_file.fclose(file_handle);  
    commit;  
end loadProce;  
 
3.调用,将TYPE BODY、PROCEDURE、TYPE、FUNCTION、TRIGGER、PACKAGE BODY、PACKAGE保存到桌面的test.sql中
Sql代码  
begin  
   loadproce('bijian');  
end;  
 
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,