Generate DDL for objects (just for fun)

I'm been putting together queries that generate DDL. Considering exp/imp ROWS=0 can be used, or DBMS_METADATA there is little point. Though, with restricted access those may not be possible options, but then an external tool like TOAD could do it. Therefore, my justification for this is "just for fun".
Here is one for TABLEs. It does not generate the CONSTRAINTs (which i try to get in the next query), and no storage clauses. This query is also formatted for fixed width font with tabs equivalent to eight characters:
SELECT
     SUBSTR
      REPLACE
      CASE
       WHEN Columns.Column_Id = 1 THEN
        'CREATE TABLE ' || Columns.Table_Name
        || CHR(10) || '('
        || CHR(10)
      END
      || ' ' || Columns.Column_Name
      || RPAD(CHR(09), Tabs - FLOOR((LENGTH(Column_Name) +1) / 8), CHR(09))
      || Data_Type
      || CASE
          WHEN Data_Type IN ('CHAR', 'VARCHAR2') THEN '(' || Char_Length || ')'
          WHEN Data_Type = 'FLOAT' THEN '(' || Data_Precision ||')'
          WHEN Data_Type = 'NUMBER' THEN
          CASE WHEN Data_Precision IS NOT NULL THEN '(' || Data_Precision
             || CASE WHEN Data_Scale IS NOT NULL THEN ', ' || Data_Scale END
           ||')'
          END
         END
      || CASE
          WHEN Data_Default IS NOT NULL
          THEN
            RPAD
             CHR(09),
             CASE Data_Type
              WHEN 'CHAR' THEN CASE Char_Length WHEN 1 THEN 2 ELSE 1 END
              WHEN 'DATE' THEN 2
              ELSE 1
             END,
             CHR(09)
           || 'DEFAULT '
           || (
            SELECT
               ExtractValue
                DBMS_XMLGEN.GetXMLType
                    SELECT
                         Data_Default
                    FROM
                         All_Tab_Columns
                    WHERE
                         Owner          = ''' || Columns.Owner || '''
                      AND     Table_Name     = ''' || Columns.Table_Name || '''
                      AND     Column_Name     = ''' || Columns.Column_Name ||'''
                'ROWSET/ROW/DATA_DEFAULT'
            FROM
               Dual
         END
      || CASE
          WHEN Columns.Column_Id = Info.Total_Columns
           THEN CHR(10) || ');' || CHR(10) || CHR(10)
          ELSE ','
         END,
         ',' || CHR(10) || CHR(10),
      ',' || CHR(10)
      1,
      181
     ) Statement
FROM
     All_Tab_Columns     Columns,
      SELECT
          Owner,
          Table_Name,
          MAX(Column_Id)                         Total_Columns,
          MAX(FLOOR((LENGTH(Column_Name) + 1) / 8)) + 1     Tabs
      FROM
          All_Tab_Columns
      WHERE
          Owner          = 'SYS'
      GROUP BY
          Owner,
          Table_Name
     )          Info
WHERE
     Columns.Owner          = Info.Owner
  AND     Columns.Table_Name     = Info.Table_Name
ORDER BY
     Columns.Owner,
     Columns.Table_Name,
     Columns.Column_Id;This next query get CONSTRAINTs. No formatting is used, because it becomes quite unweildy (though, if it used multiple lines, that would change). Another interesting thing is whether the CONSTRAINT names were generated or not. If they were generated, this still uses the old name and does not generate a new one:
WITH
     Cons_Columns
AS
      SELECT
          Owner,
          Constraint_Name,
          SUBSTR
           REPLACE
            REPLACE
             XMLAgg(XMLElement("A", Column_Name)
            '<A>',
            '</A>'
           4
          ) || '"' List
      FROM
           SELECT
               Owner,
               Constraint_Name,
               Column_Name
           FROM
               All_Cons_Columns
           ORDER BY
               Position
      GROUP BY
          Owner,
          Constraint_Name
SELECT
     'ALTER TABLE ' || Table_Name
     || ' ADD CONSTRAINT ' || Constraint_Name
     || ' '
     || CASE Constraint_Type
         WHEN 'C' THEN 'CHECK'
         WHEN 'U' THEN 'UNIQUE'
         WHEN 'P' THEN 'PRIMARY KEY'
         WHEN 'R' THEN 'FOREIGN KEY'
        END
     || '('
     || CASE
         WHEN Constraint_Type = 'C' THEN
           SELECT
               ExtractValue
                DBMS_XMLGEN.GetXMLType
                    SELECT
                         Search_Condition
                    FROM
                         All_Constraints
                    WHERE
                         Owner          = ''' || Cons.Owner || '''
                      AND     Constraint_Name     = ''' || Cons.Constraint_Name || '''
                'ROWSET/ROW/SEARCH_CONDITION'
           FROM
               Dual
        WHEN Constraint_Type IN ('P', 'R', 'U') THEN
           SELECT
               List
           FROM
               Cons_Columns
           WHERE
               Cons_Columns.Owner          = Cons.Owner
             AND     Cons_Columns.Constraint_Name     = Cons.Constraint_Name
        END     
     || ')'
     || CASE Constraint_Type
         WHEN 'R' THEN
           SELECT
               ' REFERENCES (' || List || ')'
           FROM
               Cons_Columns
           WHERE
               Cons_Columns.Owner          = Cons.R_Owner
             AND     Cons_Columns.Constraint_Name     = Cons.R_Constraint_Name
          || ' ON DELETE ' || Delete_Rule
         ELSE ''
        END
     || ' ' || DEFERRABLE || ' ' || DEFERRED
     || ' ' || VALIDATED || ' ' || STATUS || RTRIM(' ' || RELY)
     || ';'
FROM
     All_Constraints Cons
WHERE
     Owner = 'SYS'
ORDER BY
     1,
     Constraint_Name;

Shoblock, thanx!
For NUMBER is was using a wrong COLUMN, and also in the wrong order. Serves me right for not following the documentation. And, i just ignored FLOAT. But not it mostly matches DESC (except this query shows the ", 0" where DESC drop it instead).
I fixed the query above.
The XML part that gets the long has a failure. If the Data_Default contains an XML special char it will get encoded. The way to not encode it, it looks, requires PL/SQL. Which means it would not be done in one query (without a FUNCTION).
I am curious if anyone else is interested in such queries, whether for "fun" or otherwise.

Similar Messages

  • SQL*Developer 2.1 - Not Generating DDL for Different Users

    Using SQL*Developer Version 2.1.0.63 I get an error trying to generate DDL from another user, that has access to many other schemas. It looks to me like Sql Developer is calling the DBMS_METADATA package from within other PL/SQL.
    I am receiving the following error:
    ORA-31603: object "ACCOUNT_TYPE_LKP" of type TABLE not found in schema "POR_OWN"
    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
    I would receive these same errors in SQL Developer 1.5, but the DDL would still generated. It was picking something up, even though it had an error.
    Our DBA has not been able to provided a work around. He says you have to login directly as the owner of the objects to use dbms_metdata in this fashion.
    Is there any work around for this? I really need to be able to get DDL and I do not want to go back to 1.5 (I like the other new features of 2.1).
    Thanks,
    Tom

    We have several users currently using SQL Navigator and/or TOAD. We would like them to switch to SQL developer, but part of their job is to view the source of views in another schema. They have select privileges on the underlying tables and are able to see the source using other tools. Using SQL Developer, they receive on ORA-31603 because it's calling dbms_meta. Note ID 1185443.1 describes the issue and suggests granting the users the SELECT_CATALOG_ROLE.
    We are hesitant about granting this role to these users which allows access to ever 1,700 objects, plus execute privileges to 4 objects.
    Support indicated that Enhancement Request 8498115 addresses this issue.
    Is this something that may be addressed in the next release?
    Thanks,
    Paul

  • How do I generate DDL for changes in model

    Hi
    I'm using SQL Developer 4 to mantain my logical and physical diagramas. I know how to generate the diagram and generate DDL. But how do I only generate the DDL changes?
    Regards,
    Néstor Boscán

    Basically you want to compare the model with the data dictionary, see the big arrow buttons up on the main toolbar.
    In depth answer here:
    http://www.thatjeffsmith.com/archive/2012/03/diffs-and-alter-scripts-via-oracle-sql-developer-data-modeler/

  • Bug - when generating DDL for table

    I'm not sure if this has been logged yet or not, but when i click on the sql tab of a table it generates the ddl to recreate the table fine the first time.
    However if i then click on another tab and then come back to the table tab, when i go to the sql tab within this table tab the ddl is still displayed on the tab but it tries to regenerate it and then freezes, i have to kill raptor and restart it.

    I have the same problems with VIEWS.
    Reproducing the error:
    Open folder Views
    Click on a view (opens window with all the view stuff, Columns, Data etc)
    Click on tab SQL
    Copy the select part
    Open a worksheet and past the selected text
    Go back to the View tab SQL with result Raptor hangs...(Generating DLL....)

  • OWB 11.2.0.3 do not re-generate DDL on objects deploy

    Hi all,
    I've added a 2nd target location/schema (after a successful deploy on 1st target location) to our OWB project and we got an error on executing the DDL script for the dimension objects. I checked the script and found that it still contains the schema name of the 1st target and the script create/modified date are both old (time of our 1st deploy).
    Any idea & solution?
    Thanks!
    - Andrew

    Hi,
    It seems it is more of design client JVM memory issue.
    Follow the below steps:
    1. Edit the owb.conf file located in the ORACLE_HOME/owb/bin directory.
    2. Modify the value in the line "AddVMOption -Xmx768M" to increase the Heap Memory for Java, based on your physical memory.
    3. Restart the Design Center
    Make sure that you are not allocating memory gretaer than the size of the RAM on that client.
    Regards,
    Pnreddy

  • How to get "Synchronize with database" to compare/generate changes for database 'contexts'?

    Hi
    Using SDDM 3.3.
    How does one get to synchronise changes/differences in 'contexts' (defined in physical model - Oracle DB)?
    I have tried to synchronize both ways, i.e. model to db and db to model, but never does it show the DDL necessary to create the missing context.
    Yes, Context is selected/ticked under preferences for Oracle DB synchronization preferences.
    Thank you & Regards

    Hi Philip
    If I use the "generate DDL" option from the toolbar, it does generate DDL for the context I have in the physical model.  I can select/deselect them in the DDL Generation Options window.
    Why then would it not generate DDL when comparing the model with the database, i.e. using the "synchronise data dictionary to model" option?  It successfully generates a 'patch'/change script for tables, views, packages, etc.?
    Thank you & Regards
    PS.  I am asking for a way to get it to generate me a complete 'patch'/change script.

  • Generating DDL from SQL Workshop

    I'm using the Database Cloud Service and am trying to see how I can generate DDL for my tables.
    I looked into the online help, which led me to this:
    http://docs.oracle.com/cd/E37097_01/doc/doc.42/e35128/sql_utl.htm#AEUTL256
    Says to click SQL Workshop -> Utilities -> Generate DDL, but there is no Generate DDL option.  Has the option moved or the name changed?
    Thanks,
    Rick

    Hello Rick -
    The easiest way to get DDL for the tables in your Database Cloud Service is to go to the Cloud Management UI for your Service and click on the Data  Export tab. Click on the Export Data button and just do not check the box for exporting data.  This will give you DDL for your Service.
    Hope this helps.
    - Rick Greenwald

  • Generate DDL works in old version of SQL Developer, but not in new versions

    Hello all,
    How to generate DDL in new versions of Oracle SQL Developer for production Oracle database?
    For production database, the generate DDL feature works fine in old versions (version 1.5.5) of Oracle SQL Developer, SQuirrel, Toad and many other similar tools, but not the new versions of Oracle SQL Developer (such as 2.1.1.64.45).
    The SQL tab for a table is blank for me in production databases. Of course, it works fine in development Oracle database.
    I did some research that many articles describe SELECT_CATALOG_ROLE as the required read-only role in production database. Our DBA did a test to grant me this role temporarily, and it worked for me to generate DDL using new Oracle SQL Developer. It is pain for me to ask for this role.
    Is there a plug-in or work-around, so the new versions of Oracle SQL Developer can have this generate DDL feature in production without the mentioned SELECT_CATALOG_ROLE?
    I could not find the answer in older forum threads.
    For now, I will have to install older versions of Oracle SQL Developer.
    Thanks

    I've seen a couple posts about people not being able to generate DDL for other users. To date, I've never seen a anybody post a workaround, nor have I seen one of the developers post a response, nor have I seen anybody say they've submitted a SR.
    I think I remember reading somewhere that the new version of SQL Developer uses dbms_metadata.get_ddl to generate DDLS, rather than some custom query built on top of catalog views...which could explain why it use to work, but no longer does. But whether or not this is true, I don't know.
    You could try submitting a SR and see if Oracle responds saying that it's a known bug and will be fixed in the next release. Until then, unless you get the role, you're stuck using something else for DDL generation.

  • Generate DDL Performance

    Version: 1.1.2.25 Main Build: 25.79
    When selecting Generate DDL for a particular table the response is very slow and if one cancels the request SQL Developer freezes. Any thoughts/suggestions?
    Thank you.

    generate ddl is only for the data definitons language
    if you want to have complete data too
    use the exp export utility built-in with the database....\\\\\\
    Try It.
    Edited by: vansul on 22 May, 2010 1:02 PM

  • ORA-20001: Rem Unable to generate DDL

    Hi,
    I am getting the following message
    ORA-20001: Rem Unable to generate DDL for ORA-20001: WWV_FLOW_GENERATE_DDL.SAVE_DDL_ERROR .
    when I try to spawn DDL scripts for selected triggers.
    Thanks,
    Flavio

    Hi, thanks for your reply.
    Well, you're right, I was assuming this forum was dedicated to the on-line HTML DB trial environment, but probably is a more general one.
    I can reproduce easily the problem by repeating the same request of generating the DDL for the last 4 triggers shown in the list, but there is little point in giving the details unless you can check the HTML DB trial environment. I mean, I don't know if this is a generic problem or something specific to my code, and I cannot try it elsewhere.
    Thanks,
    Flavio

  • DROP SEQUENCE in generated DDL

    Hi,
    is there a way to get DROP SEQUENCE statements in the generated DDL for the sequences associated with the autoincremented columns?
    Thank you,
    Marian

    Even if you go to dynamic SQL, I believe you'll still have problems because you're not allowed to commit inside a trigger and DDL does an implicit commit before and after it is executed. I suppose you could use autonomous transactions to work around this, but that would mean that the sequence would be dropped even if the triggering operation were rolled back...
    I'll echo Kamal, though, that whatever business requirement is leading to this technical requirement needs to be seriously re-examined. Dropping and re-creating sequences is something that should really never be done on a production system.
    Justin
    Distributed Database Consulting, Inc.
    http://www.ddbcinc.com/askDDBC

  • Can you get DDL for more than a single object?

    I need to have the ability to select from the list of objects (say tables for this example) and generate the DDL for all or some of the tables. At this point I can only see how I can get the DDL for a single table.

    It's coming hopefully i'll have it done in the next EA drop.
    -kris

  • Generating Proxies for SAP Basis Objects?

    All,
    Is it possible to generate proxies for your interfaces under SAP Basis Component?
    1. I checked in SPROXY on my R3 and the SAP Basis Component is not displayed  here.What I also noticed is that none of the SWCV's containing Pre Delivered Integration Content is displayed in SPROXY.
    Is it necessary that the options , <b>All Objects are Modifiable and Original Objects</b> be selected for Proxies to be generated on the SAP Shipped SWCV's?
    2. As a work around ( one which did not work ) I created a Depenedency in the SLD for my SWCV on the SAP BASIS Swcv and I am now able to see the BASIS Objects in the IR under my Custom SWCV .
    But even then in SPROXY , (basis Objects )message interfaces are not displayed.
    Has anyone tried something of this sort? Is there some setting we need to do to enable proxy generation for SAP Delivered Components?
    Regards
    Bhavesh
    PS: I am able to see my custom SWCV in SPROXY and trigger proxy calls Successfully.
    I can generate a Custom Messge interface using the Message Type of the SAP Basis Component and create the Proxy to this Message interface . But, I just want to know if anyone has had success with the above concepts

    ><i>but they are already generated... do you see that ?</i>
    I did notice it. But didnt dig deep enough . Will give this a real close look tom morning .
    <i>>>>>Does this imply, that we cannot generae proxies on SAP Basis Components?
    indeed</i>
    That answered my Question  Thanks for the info.
    I guess I will have to create a message Interface will the Message Type from the BASIS Component and use this Message Interface.
    The only issue I am now wondering with this approach would be, what if the Message Type structure changes during some SP upgrade? Due to some new feature being added?
    Any experiences to share michal?
    Thanks a bunch for all those inputs.
    Regards
    Bhavesh

  • Error while generating DDL commands using startSQLRepository for a new Repository

    Hi,
    I am trying to generate DDL Commands using startSQLRepository for my new repository SubashRepository so that I can use them to create new table structure.
    All the repository related changes looks good. i can see my repository in ACC
    When I run the command:
    startSQLRepository -m SupremeATG –repository /com/supreme/SubashRepository /com/supreme/subashRepository.xml -outputSQLFile C:/ATG/ATG9.3/SupremeATG/config/com/supreme/subashRepositoryDDL.txt
    I get following error:
    Table 'SUBASH_MEMBER' in item-descriptor: 'member' does not exist in a table space accessible by the data source.  DatabaseMetaData.getColumns returns no columns
    Note:
    * errors related to definition file were cleared as the same command threw relevant exception while trying to store a array property with out creating a multi table.
    * Now this is the only exception i see
    * Some DDL are getting generated in the output file, but those are related to inventory repository (I am not sure why this is happening as I have specifically gave the path to my definition file).
    Any help in resolving this is highly appreciated.

    Pl post in the ATG forum

  • Hey Guys, from Germany. I want to connect my iMac 21" with my Tv just for fun. Does anyone know, how it works. With a Hdmi adapter?

    Hey guys! I want to connect my Imac 21 " with my Tv just for fun. Does anyone know , how it works?

    Selecting the correct adapter and cable depends on which year model iMac you have and what inputs the TV has?
    Check the User's Guide that came with your iMac or your Spec's at > Apple - Support - Technical Specifications
    to see if your iMac has a Mini-DVI or Mini DisplayPort and then select that adapter with the correct connection for the TV.
    Example: if you have a 2010 or 2011 iMac and an HDMI ready TV, then you would want the > Moshi Mini DP to HDMI Adapter with Audio Support - Apple Store (U.S.)

Maybe you are looking for