Get VIEW ddl using query
Asked Answered
F

2

18

For database re-architecture I need to get DDL of each table and view in the database(Oracle). I don't want to go to property of each table/view and get SQL out of it in SQL Developer.

I successfully got DDL for table using-

select dbms_metadata.get_ddl('TABLE','Table_name','Schema_Name') 
  from dual;

But facing problem with VIEW and MVIEW. Could anyone provide commands/keywords for elements other than table.

Also, I want to export the result in an excel file with first column as TableName and second column as DDL.

Feinleib answered 7/5, 2014 at 10:52 Comment(3)
Simply replace TABLE with VIEW and Table_Name with a View_Name in your dbms_metadata.get_ddl() call.Bonnee
just be aware that text of view will/could be different from originally submitted.Momentary
@NicholasKrasnov I tried the same. but its giving below error ORA-31603: object "prs_talent_assessment_vw" of type VIEW not found in schema "oradba" ORA-06512: at "SYS.DBMS_METADATA", line 5088 ORA-06512: at "SYS.DBMS_METADATA", line 7589 ORA-06512: at line 1 31603. 00000 - "object \"%s\" of type %s not found in schema \"%s\"" *Cause: The specified object was not found in the database. *Action: Correct the object specification and try the call again. But prs_talent_assessment_vw is present in db and oradba can access it.Feinleib
C
38

Try the below query for view:

select text from ALL_VIEWS where upper(view_name) like upper(<view_name>);

For mviews:

select query from ALL_MVIEWS where upper(mview_name) like upper(<mview_name>);
Charioteer answered 7/5, 2014 at 12:41 Comment(1)
This works for me SELECT text FROM ALL_VIEWS WHERE VIEW_NAME = '<view_name>Nope
R
1

For materialized views use:

select dbms_metadata.get_ddl('MATERIALIZED_VIEW','MView_name','Schema_Name') 
  from dual;

See all supported object types here: DBMS_METADATA: Object Types

Rockbottom answered 6/4, 2019 at 14:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.