0

I am doing a procedure in the oracle database that has the function of performing a kind of inventory of the objects of the database.

Basically I must get the DDL of objects of type table.

For this, I am using queries from the bank itself as:

select * from user_objects; 
select * from user_constraints;
select * from user_source;

My inventory must contain the following information:

Inventory information here.

How do I get the DDL command from objects without using the function:

dbms_metadata.get_ddl(); 

and no other ready functions from the metadata library.

I have also tried this:

SELECT 
(CASE WHEN line = 1 THEN 'create or replace ' || text ELSE text END) texto 
FROM user_source
WHERE NAME = '....'
ORDER BY line

but this command does not get the ddl of table objects.

Arthur Luiz
  • 123
  • 1
  • 4
  • 2
    Why are you trying to re-invent the wheel? You can query `dba_tables`, `dba_tab_columns`, `dba_constraints`, etc. to get information about tables and columns and construct the DDL for a table manually but that is likely to be a significant effort particularly if you need to support every last option in `create table` – Justin Cave Nov 14 '19 at 18:08
  • @JustinCave im not trying to re-invent the wheel, this is an academic work and I'm trying to do what the teacher asked – Arthur Luiz Nov 14 '19 at 18:12
  • 2
    Creating DDL statements from scratch goes far beyond an academic lesson, it's something you could spend weeks on. – Adam vonNieda Nov 14 '19 at 19:00
  • The package [DBMS_METADATA](https://docs.oracle.com/cd/B10501_01/appdev.920/a96612/d_metada.htm) was introduced in Oracle 9, so maybe your tutor worked last with Oracle 8 - try to point him to the package. While working with Oracle an important skill ist to be able to find the tools and features you need for your work and not to *workaround* features that you know that exists but you can't use them (well, assuming licences plays no role;) – Marmite Bomber Nov 15 '19 at 07:13

1 Answers1

0

For getting the DDL of views, it's very easy:

SELECT VIEW_NAME, TEXT FROM ALL_VIEWS; 

If you want it to return just the text of a particular view, then do:

SELECT TEXT FROM ALL_VIEWS
WHERE VIEW_NAME LIKE '[name_of_view]';

Getting the DDL for tables is more cumbersome, but can be done by querying the data from several system views:

ALL_TABLES
ALL_TAB_COLUMNS
ALL_COL_COMMENTS
ALL_CONSTRAINTS
ALL_CONS_COLUMNS
ALL_INDEXES
ALL_IND_COMMENTS

For example, if you wanted to get all column names and their data types for TABLE1, you would do:

SELECT COLUMN_NAME, DATA_TYPE FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME LIKE 'TABLE1';

To get a list of all constraints on a table, the query is:

SELECT * FROM ALL_CONSTRAINTS
WHERE TABLE_NAME LIKE 'TABLE1';

To get a full table definition takes a fairly good understanding of how to use these system views. A very helpful page for this can be found here: 6 Useful Oracle Data Dictionary Queries Every DBA Should Have

bk_32
  • 483
  • 1
  • 4
  • 17
  • In theory, yes. But will you handle index-organised / external / nested / object tables? How about virtual columns, `default` vs `default on null`, timestamp/interval precision, [column-level collation](https://oracle-base.com/articles/12c/column-level-collation-and-case-insensitive-database-12cr2)? Perhaps the point of the exercise is to teach the OP that it's a non-trivial task. – William Robertson Nov 14 '19 at 22:52