Search code examples
oracle-databaseviewcreate-table

How can I create a table with same attributes of a View another system delivers me?


An external system granted to my system a view named V_EXT so that I can make selections on it, reading all its content:

SELECT *
FROM V_EXT;

this view has a lot of fields and I would like to create an empty table in my system with the exactly same attributes of this view (same names and same types). Is there a way to do this without simply guessing from the content I received what each attribute is?

I am using Oracle SQL Developer.


Solution

  • With Code.

    create table objects_copy2
     as
      select *
        from all_objects
        where 1=2; -- add this line if you want NO data, otherwise you get all the data too
    

    With SQL Developer specifically, it's actually harder. You would need to find the underlying OBJECT(s) used in the query. Then look up those data types, and manually build out your CREATE TABLE statement.

    CREATE TABLE AS SELECT is the way to go. (Docs)

    Note there are some limitations, for example this won't pick up Identity Column definitions from the source table used in the view.

    An example:

    enter image description here