简体   繁体   English

如何检索用于在Oracle中创建视图的SQL?

[英]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. 在Oracle中,要检索用于创建Function,Package等的SQL,可以查询user_source视图。 However, views are not included in this view - nor do they exist in the underlying sys.source$ . 但是,视图不包含在此视图中 - 它们也不存在于基础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. 要访问视图文本,可以使用user_views.text列,但这并不准确,因为Oracle会重写部分查询,例如它会进行全局扩展。

How can I retrieve the SQL used to create a view, exactly as it was entered, without glob expansion? 如何在没有全局扩展的情况下检索用于创建视图的SQL,与输入完全一样?

You can use the following query: 您可以使用以下查询:

SELECT VIEW_NAME, TEXT
FROM USER_VIEWS;

or you can use ALL_VIEWS, as in 或者您可以使用ALL_VIEWS,如

SELECT VIEW_NAME, TEXT 
FROM ALL_VIEWS;

References : 参考文献

ALL_VIEWS on Oracle® Database Reference Oracle®数据库参考上的ALL_VIEWS

I use dbms_metadata.get_ddl as follows: 我使用dbms_metadata.get_ddl如下:

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. 对于非用户拥有的对象,可以获得系统特权SELECT_CATALOG_ROLE,并且可以查看所有对象的所有DDL。

In Oracle SQL Developer, one can see the clob in the query results window (many people have created utilities to transform clobs as well). 在Oracle SQL Developer中,可以在查询结果窗口中看到clob(许多人也创建了用于转换clob的实用程序)。

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). 大多数SQL IDE工具都有一些DDL查看机制,虽然DBMS_METADATA是种子Oracle数据库功能(一个不需要一些花哨,昂贵的工具)。

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. 将仅返回id列。 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. 我认为作为开发人员,视图的兴趣是使其有效且可运行的“选择”,编辑视图的最简单方法是使用另一个应用程序,如pl / sql Developer或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. 通过文本编辑器编辑oracle数据库中的任何对象,就像sqlplus.exe需要很长时间来完成简单的任务一样,这是一个令人头痛的方法。

在此输入图像描述

You can use data dictionary views (V$SQL, V$SQLTEXT) to see the actual SQL entered. 您可以使用数据字典视图(V $ SQL,V $ SQLTEXT)来查看输入的实际SQL。

Please see this related question . 请看相关问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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