我知道如果我需要使用Oracle DDL script
生成Oracle SQL developer
我必须遵循下一步骤
Oracle - How to generate script from sql developer
SQL Developer - >工具 - >数据库导出......
但是呢
如果我需要写脚本或任何其他技巧
以编程方式运行它
因此可以在以后自动安排和生成
答案 0 :(得分:1)
但是,如果我需要编写脚本
如果要为用户生成DDL脚本而非手动但通过脚本生成,则使用 DBMS_METADATA 。您可以将以下脚本保存为ddl_script.sql
文件,无论何时需要生成脚本,只需在 SQL Developer 中加载该文件,或将其作为脚本运行。输出将是所需的DDL脚本。
例如,以下脚本会为 USER 提供完整的 DDL :
set long 20000 longchunksize 20000 pagesize 0 linesize 1000 feedback off verify off trimspool on
column ddl format a1000
begin
dbms_metadata.set_transform_param (dbms_metadata.session_transform, 'SQLTERMINATOR', true);
dbms_metadata.set_transform_param (dbms_metadata.session_transform, 'PRETTY', true);
end;
/
variable v_username VARCHAR2(30);
exec:v_username := upper('&1');
select dbms_metadata.get_ddl('USER', u.username) AS ddl
from dba_users u
where u.username = :v_username
union all
select dbms_metadata.get_granted_ddl('TABLESPACE_QUOTA', tq.username) AS ddl
from dba_ts_quotas tq
where tq.username = :v_username
and rownum = 1
union all
select dbms_metadata.get_granted_ddl('ROLE_GRANT', rp.grantee) AS ddl
from dba_role_privs rp
where rp.grantee = :v_username
and rownum = 1
union all
select dbms_metadata.get_granted_ddl('SYSTEM_GRANT', sp.grantee) AS ddl
from dba_sys_privs sp
where sp.grantee = :v_username
and rownum = 1
union all
select dbms_metadata.get_granted_ddl('OBJECT_GRANT', tp.grantee) AS ddl
from dba_tab_privs tp
where tp.grantee = :v_username
and rownum = 1
union all
select dbms_metadata.get_granted_ddl('DEFAULT_ROLE', rp.grantee) AS ddl
from dba_role_privs rp
where rp.grantee = :v_username
and rp.default_role = 'YES'
and rownum = 1
union all
select to_clob('/* Start profile creation script in case they are missing') AS ddl
from dba_users u
where u.username = :v_username
and u.profile <> 'DEFAULT'
and rownum = 1
union all
select dbms_metadata.get_ddl('PROFILE', u.profile) AS ddl
from dba_users u
where u.username = :v_username
and u.profile <> 'DEFAULT'
union all
select to_clob('End profile creation script */') AS ddl
from dba_users u
where u.username = :v_username
and u.profile <> 'DEFAULT'
and rownum = 1
/
set linesize 80 pagesize 14 feedback on trimspool on verify on
因此可以在以后自动安排和生成
您可以安排使用 CRON 在操作系统级别生成脚本,或使用 DBMS_SCHEDULER 在数据库级别生成脚本。