Get VIEW ddl using query - sql

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.

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>);

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

Related

Is there a way to view the query or last history in a BigQuery Table?

A coworker created a table in BigQuery using "create or replace table" function. Unfortunately the query wasn't documented. I was wondering if there's a way to see the underlying query behind the table or a way to access the edit history of the table?
Use below as an example
select ddl
from `bigquery-public-data.utility_us.INFORMATION_SCHEMA.TABLES`
where table_name = 'us_county_area'
with out put like below
There are quite a number of very usefull INFORMATION SCHEMA Views to use
I would try to find this query in INFORMATION SCHEMA JOBS:
SELECT * FROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
WHERE
job_type = 'QUERY' and
statement_type = 'CREATE_TABLE_AS_SELECT' and
lower(query) LIKE '%create%table%foo%' -- replace foo with your table name

How do I write the table structure in Oracle from an existing schema?

I have so far figured out that to describe a table I can use the below:
select dbms_metadata.get_ddl('TABLE','<my table name>','<table owner>') from dual;
I also found that I can get a list of tables from the current user using the below statement:
select table_name from user_tables;
However I need to find a way to combine these two so I get a (preferably SQL file) output which basically describes all the tables in the current schema. How can I go about that?
Call dbms_metadata in your query on user_tables:
select dbms_metadata.get_ddl('TABLE',table_name,user)
from user_tables;

Create SELECT procedure/trigger to decrypt data on ORACLE

I have this query
SELECT id, UTL_I18N.RAW_TO_NCHAR(DBMS_CRYPTO.DECRYPT(password,
pkg_so_42979606.cipher_type(),
UTL_ENCODE.BASE64_DECODE(UTL_I18N.STRING_TO_RAW('pMV3D4xhyfNxp3YyfLWzAErGcKkIjK3X6uc/WIeVTls=', 'AL32UTF8'))),
'AL32UTF8') password
FROM customeren;
I want to select this table. But i don't want to write this code again and again. What i need to do? How select data from this table in easier way? Oracle 11g XE
You could create a view with the selection criteria above and then just call your select query on the view. Not sure if Oracle syntax is the same but in SQL Server it would be
CREATE VIEW myschema.SomeViewName AS
SELECT id, UTL_I18N.RAW_TO_NCHAR(DBMS_CRYPTO.DECRYPT(password,
pkg_so_42979606.cipher_type(),
UTL_ENCODE.BASE64_DECODE(UTL_I18N.STRING_TO_RAW('pMV3D4xhyfNxp3YyfLWzAErGcKkIjK3X6uc/WIeVTls=', 'AL32UTF8'))),
'AL32UTF8') password
FROM customeren;
Then you can call SELECT * FROM myschema.SomeViewName, although using wildcards is generally not a good idea because you return all columns, when you may not require them. If you want to pass in parameters dynamically then you could consider using a function instead.

SQL: how to find a mapping of view columns to table columns?

In Microsoft SQL Server 2008 R2, let's say my database has the following view:
create view [dbo].[MyView]
(
[MyColumnA]
)
AS
(SELECT MyColumnB FROM MyTable)
Now let's suppose I only know that there is a view called MyView that has a column called MyColumnA, but I don't know that it maps to MyTable.ColumnB. What is the easiest/fastest way to determine which table and column MyView.ColumnA maps to? Is there a query that can tell me this? Something like:
SELECT TABLE_NAME, TABLE_COLUMN_NAME
FROM INFORMATION_SCHEMA.VIEW_MAPPINGS
WHERE VIEW_NAME = 'MyView' AND VIEW_COLUMN_NAME = 'MyColumnA'
This query would return [MyTable, MyColumnB].
Currently I have to find the view in SSMS Object Explorer, right click it and generate the create script, then search for the name of the view's column. Then I note which ordinal position it is in the view (let's say 4th column), and have to find the corresponding 4th column in the select statement. The select statement will most likely be using a table alias, so then I have to look through the JOIN statements to find the table name based on the alias.
This is quite time consuming, and I'm hoping to find a faster way, if not by a query then perhaps by some other process that is faster or easier than mine.
SP_DEPENDS should work
SP_DEPENDS 'MyView'

Copy a table including DDL statements for an Oracle table

I Know of this below mentioned command for copying table fully but i am not able to figure out how this command will work because i cant just find the mentioning of newly created table. (courtesy SO).
SET LONG 5000
SELECT dbms_metadata.get_ddl( 'TABLE', 'MY_OLD_TABLE_NAME' ) FROM DUAL;
And also i read in some oracle forum posts that this is not a one shot solution to copy table ,its data and all the constraints , triggers , indexes and other such objects.
IS this true ?
Simplest way to export is use pl/sql developer or sql developer and use export data functionality. They export entire table with constraints, indexes, data. Just give it a try.
I am not sure what you meant by "copy a table including DDL statements". If you are looking to create a new table using dbms_metadata.get_ddl, then the above command will actually need to be modified as (not tested) :-
Assuming that variable tbl_sql has been defined in plsql block having length of 32767, you can do :-
SELECT REPLACE(DBMS_LOB.SUBSTR(dbms_metadata.get_ddl('TABLE','MY_OLD_TABLE_NAME'),32760,1),'MY_OLD_TABLE_NAME','NEW_TABLE_NAME') INTO tbl_sql FROM DUAL;
EXECUTE IMMEDIATE tbl_sql;
If you want to create a new table, then the following might be even better :-
EXECUTE IMMEDIATE 'CREATE TABLE NEW_TABLE_NAME AS SELECT * FROM MY_OLD_TABLE_NAME';
The dbms_metadata.get_ddl statement will not copy your data. However, the second statement (CTAS) will copy your data also.
Both the statements do not copy the constraints (CTAS does copy NOT NULL constraints) and indexes (as far as I know). To copy indexes, you will have to execute the dbms_metadata.get_ddl statement passing 'INDEX' as the first argument.
SELECT REPLACE(DBMS_LOB.SUBSTR(dbms_metadata.get_ddl('INDEX','MY_OLD_INDEX_NAME'),32760,1),'MY_OLD_INDEX_NAME','NEW_INDEX_NAME') INTO idx_sql FROM DUAL;
EXECUTE IMMEDIATE idx_sql;
keep it simple
create table xyz_new as select * from xyz where 1=0;
Create table with indexes ( More useful )
create table xyz_new like xyz