I'm trying to get the DDL for tables, views, indexes, sequences and so on in order to use for version control, and do comparisons between different db's which should have identical tables (i.e. dev vs uat).
I would like to be able to sort by table vs. view vs. index...and then sort within those by table name, or view name.
This will help with comparison. Basically taking this post and extending functionality: Problem with getting an Oracle table ddl but with out the extra detail from the current schema
Expected output is -a clob of all the data that I can use for comparison, version control, and usable as a ddl script (create tables once in dev, dump them into UAT after testing)
I've got a function
create or replace FUNCTION F_clean_DDL RETURN VARCHAR2 AS
BEGIN
dbms_metadata.set_transform_param (
dbms_metadata.session_transform, 'SQLTERMINATOR', true);
dbms_metadata.set_transform_param (
dbms_metadata.session_transform, 'SQLTERMINATOR', true);
dbms_metadata.set_transform_param (
dbms_metadata.session_transform, 'PRETTY', true);
dbms_metadata.set_transform_param (
dbms_metadata.session_transform, 'SEGMENT_ATTRIBUTES', false);
dbms_metadata.set_transform_param (
dbms_metadata.session_transform, 'CONSTRAINTS_AS_ALTER', false);
dbms_metadata.set_transform_param (
dbms_metadata.session_transform, 'STORAGE', false);
RETURN 'F_clean_DDL.';
END;
And I've got the following view:
SELECT CONCAT('1_', table_name) AS renamed_table_name, DBMS_METADATA.GET_DDL('TABLE', table_name) AS ddl
FROM user_tables
UNION ALL
SELECT CONCAT('2_', view_name) AS renamed_view_name, DBMS_METADATA.GET_DDL('VIEW', view_name) AS ddl
FROM user_views
UNION ALL
SELECT CONCAT('3_', index_name) AS renamed_index_name, DBMS_METADATA.GET_DDL('INDEX', index_name) AS ddl
FROM user_indexes
UNION ALL
SELECT CONCAT('4_', sequence_name) AS renamed_sequence_name, DBMS_METADATA.GET_DDL('SEQUENCE', sequence_name) AS ddl
FROM user_sequences
UNION ALL
SELECT CONCAT('5_', mview_name) AS renamed_mview_name, DBMS_METADATA.GET_DDL('MATERIALIZED_VIEW', mview_name) AS ddl
FROM user_mview
ORDER BY RENAMED_TABLE_NAME DESC
I get the results I want with 'select DDL from v_ddl ORDER BY renamed_table_name ASC'
What is the best way to combine executing the function and then the view into 1 step? I'm sure its a function or stored procedure but I believe my approach may be wrong.