从Oracle数据库中导出SQL脚本
基本上用到的语法如下:
a. 获取单个的建表和建索引的语法
set heading off;
set echo off;
Set pages 999;
set long 90000;
spool DEPT.sql
select dbms_metadata.get_ddl('TABLE','DEPT','SCOTT') from dual;
select dbms_metadata.get_ddl('INDEX','DEPT_IDX','SCOTT') from dual;
spool off;
b.获取一个SCHEMA下的所有建表和建索引的语法,以scott为例:
set pagesize 0
set long 90000
set feedback off
set echo off
spool scott_schema.sql
connect scott/tiger;
SELECT DBMS_METADATA.GET_DDL('TABLE',u.table_name)
FROM USER_TABLES u;
SELECT DBMS_METADATA.GET_DDL('INDEX',u.index_name)
FROM USER_INDEXES u;
spool off;
c. 获取某个SCHEMA的建全部存储过程的语法
connect brucelau /brucelau;
spool procedures.sql
select
DBMS_METADATA.GET_DDL('PROCEDURE',u.object_name)
from
user_objects u
where
object_type = 'PROCEDURE';
spool off;
另:
dbms_metadata.get_ddl('TABLE','TAB1','USER1')
三个参数中,第一个指定导出DDL定义的对象类型(此例中为表类型),第二个是对象名(此例中即表名),第三个是对象所在的用户名。.
a. 获取单个的建表和建索引的语法
set heading off;
set echo off;
Set pages 999;
set long 90000;
spool DEPT.sql
select dbms_metadata.get_ddl('TABLE','DEPT','SCOTT') from dual;
select dbms_metadata.get_ddl('INDEX','DEPT_IDX','SCOTT') from dual;
spool off;
b.获取一个SCHEMA下的所有建表和建索引的语法,以scott为例:
set pagesize 0
set long 90000
set feedback off
set echo off
spool scott_schema.sql
connect scott/tiger;
SELECT DBMS_METADATA.GET_DDL('TABLE',u.table_name)
FROM USER_TABLES u;
SELECT DBMS_METADATA.GET_DDL('INDEX',u.index_name)
FROM USER_INDEXES u;
spool off;
c. 获取某个SCHEMA的建全部存储过程的语法
connect brucelau /brucelau;
spool procedures.sql
select
DBMS_METADATA.GET_DDL('PROCEDURE',u.object_name)
from
user_objects u
where
object_type = 'PROCEDURE';
spool off;
另:
dbms_metadata.get_ddl('TABLE','TAB1','USER1')
三个参数中,第一个指定导出DDL定义的对象类型(此例中为表类型),第二个是对象名(此例中即表名),第三个是对象所在的用户名。.
要自己写程序了,执行一段SQL,让SQL查询结果就是我们想要的SQL脚本:
如下:
select
'INSERT INTO B_STATTEMPLATE ( N_ID,C_NAME, C_KBH, N_PRINT, N_TYPE, N_APP, N_VALID ) '
|| 'Values (' || To_Char(N_ID) ||',''' || C_NAME || ''', ''' || C_KBH || ''', '
|| To_Char( N_PRINT ) || ', ' || To_Char( N_TYPE ) || ', ' || '0, 0 );'
from b_stattemplate
'INSERT INTO B_STATTEMPLATE ( N_ID,C_NAME, C_KBH, N_PRINT, N_TYPE, N_APP, N_VALID ) '
|| 'Values (' || To_Char(N_ID) ||',''' || C_NAME || ''', ''' || C_KBH || ''', '
|| To_Char( N_PRINT ) || ', ' || To_Char( N_TYPE ) || ', ' || '0, 0 );'
from b_stattemplate