0

I knew If I need to generate Oracle DDL script using Oracle SQL developer
I must follow the next steps
Oracle - How to generate script from sql developer
SQL Developer -> Tools -> Database Export......

But what about if I need to write script or any other trick
To run it PROGRAMMATICALLY
So it can be scheduled and generated automatically later

3
  • It it a requirement to use SQL Developer as proxy? Commented Mar 16, 2015 at 10:54
  • We have a new version of SQL Developer, SQLCl - which is a new kind of SQL*Plus. It has a new command, 'DDL' which generates the ddl as expected, which you can then wrap with a SPOOL command. It's in beta, but i have a slidedeck and video here thatjeffsmith.com/archive/2015/02/… Commented Mar 16, 2015 at 13:04
  • This might help someone just simply write table_name in sql editor then --> select table name --> ​right click ​--> open declaration ​--> go to sql tab Commented Oct 21, 2021 at 16:06

1 Answer 1

1

But what about if i need to write script

If you want to generate a DDL script for a user not manually but via script, then use DBMS_METADATA. You could save the below script as a ddl_script.sql file and whenever you need to generate the script, simply load the file in SQL Developer or run it as script. The output will be the required DDL script.

For example, the following script would give you the complete DDL for a USER:

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

Source link.

So it can be scheduled and generated automatically later

You could schedule to generate the script at OS level using CRON or at database level using DBMS_SCHEDULER.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.