Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is SQLPlus command to view a VIEW statement?

Tags:

sql

oracle

I just found out that one of the queries I am running is not coming from actual table, it is coming from a VIEW.

I would like to see the SQL query in that VIEW. If I try to describe I get an object does not exist error. When I SELECT from the view I get some data.

like image 752
Mowgli Avatar asked Oct 01 '12 19:10

Mowgli


People also ask

How do I display a view in SQLPlus?

replace VIEW_NAME with the name of your view. Object_type='VIEW' should not be changed. select * from schema. view_name; , schema is the DB User or owner.

What is views in SQLPlus?

Views in SQL are kind of virtual tables. A view also has rows and columns as they are in a real table in the database. We can create a view by selecting fields from one or more tables present in the database. A View can either have all the rows of a table or specific rows based on certain condition.

Can you query a view in Oracle?

A view is a virtual table because you can use it like a table in your SQL queries. Every view has columns with data types so you can execute a query against views or manage their contents (with some restrictions) using the INSERT , UPDATE , DELETE , and MERGE statements. Unlike a table, a view does not store any data.


2 Answers

To see the SQL underlying a view, you'll need to query the data dictionary. Try

select view_name, text from user_views where view_name = 'MY_VIEW';

If your user does not own the view, then try all_views;

like image 179
Wolf Avatar answered Nov 04 '22 21:11

Wolf


If you would like to see the actual SQL used to create the view you can use the function dbms_metadata.get_ddl, which returns a clob:

select dbms_metadata.get_ddl ( 'VIEW'
                             , 'MY_VIEW' -- view name
                                )
  from dual

There are a few more options if you need them.

If you want to just describe it as usual. If this is not working you're in the wrong schema or the object does not exist:

DESC MY_VIEW

If you're in the incorrect schema you can use:

select dbms_metadata.get_ddl ( 'VIEW'
                             , 'MY_VIEW', -- view name
                             , 'MY_SCHEMA'
                                )
  from dual

or

DESC MY_SCHEMA.MY_VIEW
like image 35
Ben Avatar answered Nov 04 '22 23:11

Ben