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

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;

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

Write a query to find all the views in a specific schema which is making use of two tables that are in a different schema in snowflake

I have a Database ( say x_db ) which contains two schemas ( a_db, b_db ). There are two tables t1 and t2 in b_db but this schema doesn't have any views. Can you help me write a query which lists all the views in the a_db schema which uses both the tables t1 and t2 either directly or indirectly in snowflake?
select table_catalog as database_name,
table_schema as schema_name,
table_name as view_name,
created
from x_db.information_schema.views
where table_schema= 'a_db' and view_definition LIKE '%t1%'
or view_definition LIKE '%t2%'
order by table_name;
I have tried this code so far;
The code in the question looks good, and now you have to figure out how to parse the table names out of view_definition.
An alternative way would be to obtain the plan of running a view.
For example:
create or replace view delete_view
as select id
from wikimedia.public.wikidata_by_instance
limit 10;
explain select * from delete_view;
select "objects"
from table(result_scan(last_query_id()))
where "operation"='TableScan'
The result is: WIKIMEDIA.PUBLIC.WIKIDATA_BY_INSTANCE.
The steps to pull this off would be:
Find all views in a schema from the INFORMATION_SCHEMA (you already figured this out).
Run a EXPLAIN SELECT * for each view (or combine all of them in a join/union).
Use the code in this question to scan the objects that go through a TableScan when running the previous explain query.

Get VIEW ddl using query

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

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

SQL/JDBC : select query on variable tablenames

I'm using Oracle DB and I would like to write a SQL query that I could then call with JDBC. I'm not very familiar with SQL so if someone can help me, that could be great ! Here is the problem. I have a table MY_TABLE wich contains a list of another tables, and I would like to keep only the nonempty tables and those that their names start by a particular string.
The query I wrote is the following :
select TABLE_NAME
from MY_TABLE
where TABLE_NAME like '%myString%'
and (select count(*) from TABLE_NAME where rownum=1)<>0
order by TABLE_NAME;`
The problem comes from the second SELECT, but I don't know how can I do to use the TABLE_NAME value.
Does someone have an idea ?
Thanks.
[Added from comments]
Actually, I need to test the V$ views contained in the ALL_CATALOG table. But if I can find another table where all these views are contained too and with a NUM_ROWS column too, it would be perfect !
Standard versions of SQL do not allow you to replace 'structural elements' of the query, such as table name or column name, with variable values or place-holders.
There are a few ways to approach this.
Generate a separate SQL statement for each table name listed in MY_TABLE, and execute each in turn. Brute force, but effective.
Interrogate the system catalog directly.
Investigate whether there are JDBC metadata operations that allow you to find out about the number of rows in a table without being tied to the system catalog of the specific DBMS you are using.
Can you use oracle view USER_TABLES? then query will be much easier
select TABLE_NAME
from USER_TABLES
where TABLE_NAME like '%myString%'
and Num_ROWS > 0
order by TABLE_NAME;`