--Oracle如何批量导出数据库表结构(建表语句) --1. 用system用户创建路径 utl_dir用于存放文件 -- D:\hsx\dba必需是已经存在的目录 create or replace directory UTL_DIR as 'd:/hsx/dba'; --用sys用户登录给要访问的用户指定访问目录的权限 grant write on directory UTL_DIR to public; grant read on directory UTL_DIR to public; --2. 在需要导出表结构的用户下创建存储过程(导出单个表结构) CREATE OR REPLACE PROCEDURE P_EXPORTDLL(P_TABLE_NAME VARCHAR2, P_FILENAME VARCHAR2) IS BEGIN DECLARE L_FILE UTL_FILE.FILE_TYPE; L_BUFFER VARCHAR2(1000); L_AMOUNT BINARY_INTEGER := 100; L_POS INTEGER := 1; L_CLOB CLOB; L_CLOB_LEN INTEGER; BEGIN SELECT DBMS_METADATA.GET_DDL('TABLE', P_TABLE_NAME) || ';' INTO L_CLOB FROM DUAL; L_CLOB_LEN := DBMS_LOB.GETLENGTH(L_CLOB); L_FILE := UTL_FILE.FOPEN('UTL_DIR', P_FILENAME || '.sql', 'a', 1000); WHILE L_POS < L_CLOB_LEN LOOP DBMS_LOB.READ(L_CLOB, L_AMOUNT, L_POS, L_BUFFER); UTL_FILE.PUT(L_FILE, L_BUFFER); L_POS := L_POS + L_AMOUNT; END LOOP; UTL_FILE.FCLOSE(L_FILE); END; END P_EXPORTDLL; --3. 创建导出所有结构的存储过程函数 CREATE OR REPLACE PROCEDURE P_WHOLE AS BEGIN FOR X IN (SELECT TABLE_NAME FROM USER_TABLES) LOOP P_EXPORTDLL(X.TABLE_NAME, 'paul'); END LOOP; END; --4. 调用存储过程,批量导出表结构 --EXEC P_WHOLE; begin P_WHOLE; commit; end;