0

How create a stored procedure for read the tables into a schema, and for all tables create a .sql file with the DDL (only CREATE TABLE and PRIMARY KEY).

The input parameter is the schema.

Thank you so much

  • 2
    No need to write your own. Oracle already did that for you [dbms_metadata in the Oracle manual](https://docs.oracle.com/database/121/ARPLS/d_metada.htm) –  Sep 11 '18 at 14:39
  • Thanks, but I need the entire code for all the procedure... – Noyzware Sep 11 '18 at 14:49

1 Answers1

0

You'll need to change 'MY_DIRECTORY' to an Oracle Directory object that you can write to. Also, this'll take a little while to run - GET_DDL is not fast.

create or replace procedure table_ddl_to_file(schema_name in varchar2)
is
    v_clob clob := null;
begin
    for t in (select distinct table_name from all_tables where owner = schema_name)
    loop
        v_clob := v_clob || DBMS_METADATA.GET_DDL('TABLE',t.table_name, schema_name) || chr(10);
    end loop;

    DBMS_XSLPROCESSOR.CLOB2FILE(v_clob, 'MY_DIRECTORY', schema_name || '_table_ddl.sql');
end;
/
kfinity
  • 8,581
  • 1
  • 13
  • 20