Currently there are about 30 tables in the Oracle 11.1 database.
Is there a way to generate all ddl with a single command? (Or a few commands?)
Edit:
Following a suggestion below, I tried:
SELECT dbms_metadata.get_ddl( 'TABLE', table_name, owner )
FROM all_tables;
And got:
ORA-31603: object "HS_PARTITION_COL_NAME" of type TABLE not found in schema "SYS"
ORA-06512: at "SYS.DBMS_SYS_ERROR", line 105
ORA-06512: at "SYS.DBMS_METADATA", line 3241
ORA-06512: at "SYS.DBMS_METADATA", line 4812
ORA-06512: at line 1
31603. 00000 - "object \"%s\" of type %s not found in schema \"%s\""
*Cause: The specified object was not found in the database.
*Action: Correct the object specification and try the call again.
It's clear that there is something extremely basic about dbms_metadata that I don't understand.
Here's what worked for me:
SELECT dbms_metadata.get_ddl('TABLE', table_name)
FROM user_tables;
You can use the DBMS_METADATA package. Something like
SELECT dbms_metadata.get_ddl( 'TABLE', table_name, owner )
FROM all_tables
WHERE <<some condition to get the 30 tables in question>>
Yes you can pretty easily using the dbms_metadata package. You can write a routine that opens a cursor on the USER_TABLES system table and gets the ddl for each table. An example for that is in the article too.
If you want to individually generate ddl for each object,
Queries are:
--GENERATE DDL FOR ALL USER OBJECTS
--1. FOR ALL TABLES
SELECT DBMS_METADATA.GET_DDL('TABLE', TABLE_NAME) FROM USER_TABLES;
--2. FOR ALL INDEXES
SELECT DBMS_METADATA.GET_DDL('INDEX', INDEX_NAME) FROM USER_INDEXES WHERE INDEX_TYPE ='NORMAL';
--3. FOR ALL VIEWS
SELECT DBMS_METADATA.GET_DDL('VIEW', VIEW_NAME) FROM USER_VIEWS;
OR
SELECT TEXT FROM USER_VIEWS
--4. FOR ALL MATERILIZED VIEWS
SELECT QUERY FROM USER_MVIEWS
--5. FOR ALL FUNCTION
SELECT DBMS_METADATA.GET_DDL('FUNCTION', OBJECT_NAME) FROM USER_PROCEDURES WHERE OBJECT_TYPE = 'FUNCTION'
GET_DDL Function doesnt support for some object_type like LOB,MATERIALIZED VIEW, TABLE PARTITION
SO, Consolidated query for generating DDL will be:
SELECT OBJECT_TYPE, OBJECT_NAME,DBMS_METADATA.GET_DDL(OBJECT_TYPE, OBJECT_NAME, OWNER) FROM ALL_OBJECTS WHERE (OWNER = 'XYZ') AND OBJECT_TYPE NOT IN('LOB','MATERIALIZED VIEW', 'TABLE PARTITION') ORDER BY OBJECT_TYPE, OBJECT_NAME;
Related
Hi I have a requirement to scan through the schema and identify the tables which are redundant (candidate for dropping) ,so i did a select in DBA_Dependencies to check whether the tables are being used in any of the DB object types like (Procedure, package body, views, Materialized views....) i was able to find some tables and excluded the tables ,since i also need to capture the total counts, when the table was last loaded/used is there a automated way to select only selected tables (not found in dependencies list) and capture the counts and also when it was used/loaded
Difficulty - so many tables 500+
i have used the below query
Query 1
select table_name,
to_number(extractvalue(xmltype(dbms_xmlgen.getxml('select count(*) c from '||owner||'.'||table_name)),'/ROWSET/ROW/C')) as count
from all_tables
where owner = 'SCHEMA_NAME'
Query 2
select owner, table_name, num_rows, sample_size, last_analyzed from all_tables;
Query 1 Result
Filter Table_name=CUST_ORDER
OWNER TABLE_NAME COUNT SAMPLE_SIZE LAST_ANALYZED
ABCD CUST_ORDER 1083 1023 01.01.2020
Query 2 Result
Filter Table_name=CUST_ORDER
OWNER TABLE_NAME NUM_ROWS SAMPLE_SIZE LAST_ANALYZED
ABCD CUST_ORDER 1023 1023 01.01.2020
Question
Query 1 - Results not matching when compared with query 2 ,since the same table and filter is applied
in both the queries and why the results are not matching ?
but when i randomly checked other filter it is matching , does any one know the reason ?
Upon further testing i encountered an error ,what does this error signify permissions ?
ORA-29913: error in executing ODCIEXTTABLEOPEN callout
ORA-29400: data cartridge error
KUP-04040: file **-**.csv in ****_***_***_***** not found
29913. 00000 - "error in executing %s callout"
*Cause: The execution of the specified callout caused an error.
*Action: Examine the error messages take appropriate action.
The number you see on all_tables is a point in time capture of the number of rows. It will only be updated if the statistics are rebuilt for that table.
Here is an example:
CREATE TABLE t1 AS
SELECT *
FROM all_objects;
SELECT t.num_rows
FROM all_tables t
WHERE t.table_name = 'T1';
-- 78570
SELECT COUNT(*)
FROM t1;
-- 78570
The stats and the physical number of rows match!
INSERT INTO t1
SELECT *
FROM all_objects ao
WHERE rownum <= 5;
-- 5 rows inserted
SELECT t.num_rows
FROM all_tables t
WHERE t.table_name = 'T1';
-- 78570
SELECT COUNT(*)
FROM t1;
-- 78575
Here we have the mis-match because rows were inserted (or maybe even deleted), but the stats for the table have not been updated. Let's update them:
BEGIN
dbms_stats.gather_table_stats(ownname => 'SCHEMA',
tabname => 'T1');
END;
/
SELECT t.num_rows
FROM all_tables t
WHERE t.table_name = 'T1';
-- 78575
Now you can see the rows match. Using the value from all_tables may be good enough for your research (and will certainly be faster to query than counting every table).
Query - 1 is actual data of the table and hence it is accurate data. One can rely on this query's output.
Query - 2 is not actual data. It is the data captured when table was last analyzed and one should not be dependant on this query for finding number of records in the table.
You can gather the stats on this table and execute the query-2 then you will find the same data as query-1
If records are not inserted or deleted from the table after stats are gathered, then query-1 and query-2 data will match for that table.
In Oracle 12c, I have a table created with sharing = metadata. Following are the sql statements:
create table fedcommusr.md_commtab1 sharing=metadata
(deptno number, dname varchar2(100));
insert into fedcommusr.md_commtab1 values (1, 'One');
insert into fedcommusr.md_commtab1 values (2, 'Two');
comment on column fedcommusr.md_commtab1.deptno is 'department number';
comment on column fedcommusr.md_commtab1.dname is 'Department name is';
Executed the DBMS_STATS as follows:
exec DBMS_STATS.GATHER_SCHEMA_STATS(ownname=>'FEDCOMMUSR');
Following is the query executed to obtain the num_rows
select owner,table_name, NUM_ROWS from dba_tables where owner like upper('%fed%') ;
and output is as follows:
FEDCOMMUSR MD_COMMTAB1 (null)
Why are the num_rows not updated ?
In 12.2 latest RU I tested and have no problem: stats gathered and visible on application root as well as application PDB.
You can trace statistics gathering with dbms_stats.set_global_prefs('trace',1+4) and set serveroutput on to show it.
Regards,
Franck.
SQL Developer shows the creation and last DDL time for a public synonym in a table:
CREATED 15-AUG-09
LAST_DDL_TIME 15-AUG-09
OWNER PUBLIC
SYNONYM_NAME ISEMPTY
TABLE_OWNER MDSYS
TABLE_NAME OGC_ISEMPTY
DB_LINK (null)
How can I get the same information via a SQL query?
select * from all_synonyms where synonym_name = 'ISEMPTY'
does not get the created/last ddl dates.
More generally, is there a good way to see the queries that sql developer uses to display the data it displays (when you do not have access to a profiler)?
Thanks
You need the ALL_OBJECTS system view:
select *
from all_objects
where owner = 'OWNER_NAME'
and object_name = 'ISEMPTY'
and object_type = 'SYNONYM'
This is a pretty silly one, but I need help.
I have a table owned by mydbowner. It is named mydbowner.mytable. I tried to make a public synonym by issuing the command:
CREATE OR REPLACE PUBLIC SYNONYM mytable FOR mydbowner.mytable;
When I do this, and I query the table I get:
ORA-01775: looping chain of synonyms
How do I make this synonym without having the problem.
I think Justin is on the right track. What I think it actually means is that mydbowner.mytable doesn't exist.
Here's an example:
SQL> conn mbobak
Enter password:
Connected.
SQL> drop table mytable;
drop table mytable
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> create public synonym mytable for mbobak.mytable;
Synonym created.
SQL> select * from mytable;
select * from mytable
*
ERROR at line 1:
ORA-01775: looping chain of synonyms
I think what's happening is that Oracle tries to resolve mytable, there is no mytable in mbobak schema, so it looks for it in PUBLIC, it finds it, and sees that it points to mbobak.mytable. But, mbobak.mytable doesn't exist, so, it looks for mytable in PUBLIC, and there's the loop.
And in fact, if you create mytable, the error goes away:
SQL> create table mytable as select * from dual;
Table created.
SQL> select * from mytable;
D
-
X
1 row selected.
SQL> drop table mytable;
Table dropped.
SQL> select * from mytable;
select * from mytable
*
ERROR at line 1:
ORA-01775: looping chain of synonyms
Yes, I realize that doesn't really entirely make sense, as, once the public synonym resolved to mbobak.mytable, and that's not found, it seems to me, it should return an error ORA-942 "table or view does not exist", which makes far more sense to me.
But, this does seem to be how it works.
QED
Hope that helps.
The error you're getting implies that mydbowner.mytable is not, in fact a table. What does
SELECT object_type
FROM all_objects
WHERE owner = 'MYDBOWNER'
AND object_name = 'MYTABLE'
return?
I'd like to create ddl scripts for most of my database objects. dbms_metadata.get_ddl works for most of the object types. For instance the following creates the ddl for a view:
select dbms_metadata.get_ddl ( 'VIEW', 'SAMPLE_VIEW') from dual
On the other hand it's not working for object_type 'JOB'. The following:
select dbms_metadata.get_ddl( 'JOB', 'SAMPLE_JOB' ) from dual
gives the following error:
ORA-31604: invalid NAME parameter "NAME" for object type JOB in function SET_FILTER
ORA-06512: at "SYS.DBMS_SYS_ERROR", line 116
ORA-06512: at "SYS.DBMS_METADATA_INT", line 4705
ORA-06512: at "SYS.DBMS_METADATA_INT", line 8582
ORA-06512: at "SYS.DBMS_METADATA", line 2882
ORA-06512: at "SYS.DBMS_METADATA", line 2748
ORA-06512: at "SYS.DBMS_METADATA", line 4333
ORA-06512: at line 1
If I list my jobs using
select * from user_objects where object_type='JOB'
it shows SAMPLE_JOB (just like it shows SAMPLE_VIEW if filtered for object_type='VIEW').
Why is it working for VIEW (and TABLE, INDEX, TRIGGER, ...) and not for JOB?
I'm using Oracle 10g.
select dbms_metadata.get_ddl('PROCOBJ', 'yourJobNameGoesHere') from dual;
PROCOBJ's are procedural objects.
select dbms_metadata.get_ddl('PROCOBJ',['JOB'|'PROGRAM'|'SCHEDULE'],'OWNER') from dual;
The PROCOBJ can be JOB, PROGRAM and SCHEDULE.
Alternative, get all jobs from the database with their DDL:
select owner, job_name, dbms_metadata.get_ddl('PROCOBJ', job_name, owner) as ddl_output from ALL_SCHEDULER_JOBS
Even I tried all above to get DDL in Oracle version 10g, but no success.
Here is what I figure out to get the detail of the job:
set pages 200 lines 200
col owner format a20
col job_name format a25
col JOB_ACTION format a75
col COMMENTS format a60
select owner, job_name, next_run_date, state, enabled from dba_scheduler_jobs where job_name like '%AUDIT%';
-- get the detail of scheduled jobs.
select OWNER,JOB_NAME, JOB_ACTION, COMMENTS FROM DBA_SCHEDULER_JOBS where JOB_NAME='PURGE_AUDIT_LOG';
-- get the limited detail from the selected column.
select * FROM DBA_SCHEDULER_JOBS where JOB_NAME='PURGE_AUDIT_LOG';
-- to get the complete detail of a specific job along with code and other details.