简体   繁体   中英

How to retrieve the SQL used to create a view in Oracle?

In Oracle, to retrieve the SQL used to create a Function, Package, etc, the user_source view can be queried. However, views are not included in this view - nor do they exist in the underlying sys.source$ . To access the text of views, the user_views.text column can be used, but this is not exact because Oracle will re-write some parts of the query, for example it will do glob expansion.

How can I retrieve the SQL used to create a view, exactly as it was entered, without glob expansion?

You can use the following query:

SELECT VIEW_NAME, TEXT
FROM USER_VIEWS;

or you can use ALL_VIEWS, as in

SELECT VIEW_NAME, TEXT 
FROM ALL_VIEWS;

References :

ALL_VIEWS on Oracle® Database Reference

I use dbms_metadata.get_ddl as follows:

select
dbms_metadata.get_ddl('VIEW', 'VIEW_NAME', 'VIEW_OWNER') 
from
dual;

Here are the parameter:

DBMS_METADATA.GET_DDL (
object_type     IN VARCHAR2,
name            IN VARCHAR2,
schema          IN VARCHAR2 DEFAULT NULL,
version         IN VARCHAR2 DEFAULT 'COMPATIBLE',
model           IN VARCHAR2 DEFAULT 'ORACLE',
transform       IN VARCHAR2 DEFAULT 'DDL')
RETURN CLOB;

It has a public synonym. For objects not owned by user, one can obtain the system privilege, SELECT_CATALOG_ROLE, and one can see all DDL for all objects.

In Oracle SQL Developer, one can see the clob in the query results window (many people have created utilities to transform clobs as well).

Most SQL IDE tools have some DDL viewing mechanism, though DBMS_METADATA is seeded Oracle database functionality (one does not need some fancy, expensive tool).

I think the original text is lost:

create table t1(id number)
/
create view t1_vw as select * from t1
/
alter table t1 add val varchar2(20)
/
alter view t1_vw compile
/
select * from t1_vw
/

will return only id column. Interesting, but for materialized views original text is preserved.

I think as developer the interest of view is the "select" which make it valid and runnable, and the easiest way to edit a view is by using another application like pl/sql Developer or by TOAD.

editing any object in oracle database by text editor only like sqlplus.exe takes a long time for simple tasks, and it is a headache way to do so.

在此输入图像描述

You can use data dictionary views (V$SQL, V$SQLTEXT) to see the actual SQL entered.

Please see this related question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM