<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to extract DDL from tables/views using a SQL query? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-extract-ddl-from-tables-views-using-a-sql-query/m-p/7403#M3291</link>
    <description>&lt;P&gt;@Richard Architect​&amp;nbsp;Hi, If you are using Unity Catalog, then the Information Schema is available which you can use to get a set of views describing the objects known to the schema’s catalog. But in that as well, you can only get View definition and not table DDL I believe. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But the table DDL can be obtained programmatically by using the below code. Please see if this helps. &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;dbs = spark.catalog.listDatabases()
for db in dbs:
  tables = spark.catalog.listTables(db.name)
  for t in tables:
    DDL = spark.sql("SHOW CREATE TABLE {}.{}".format(db.name, t.name))
    print(DDL.first()[0])&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 21 Mar 2023 08:25:21 GMT</pubDate>
    <dc:creator>pvignesh92</dc:creator>
    <dc:date>2023-03-21T08:25:21Z</dc:date>
    <item>
      <title>How to extract DDL from tables/views using a SQL query?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-extract-ddl-from-tables-views-using-a-sql-query/m-p/7402#M3290</link>
      <description>&lt;P&gt;I want to write a SQL query that queries the information_schema to generate a list of objects, their columns, relationships etc.  - basically a data dictionary.  &lt;/P&gt;&lt;P&gt;For each object I want to show the DDL code, and I know I can get it by executing &lt;/P&gt;&lt;P&gt;show create table [tablename]&lt;/P&gt;&lt;P&gt;but this command only runs as standalone, not as subquery within DML.&lt;/P&gt;&lt;P&gt;I can't manually repeat 'show create table' for hundreds of tables each time, so is there a way to get to the object DDL within a SQL query?  For example in Oracle I could use a subquery &lt;/P&gt;&lt;P&gt;SELECT DBMS_METADATA.get_ddl etc. etc.&lt;/P&gt;&lt;P&gt;Can't use Python for a number of reasons. Thanks for any suggestion!&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2023 04:07:59 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-extract-ddl-from-tables-views-using-a-sql-query/m-p/7402#M3290</guid>
      <dc:creator>RichardSCarchit</dc:creator>
      <dc:date>2023-03-21T04:07:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract DDL from tables/views using a SQL query?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-extract-ddl-from-tables-views-using-a-sql-query/m-p/7403#M3291</link>
      <description>&lt;P&gt;@Richard Architect​&amp;nbsp;Hi, If you are using Unity Catalog, then the Information Schema is available which you can use to get a set of views describing the objects known to the schema’s catalog. But in that as well, you can only get View definition and not table DDL I believe. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But the table DDL can be obtained programmatically by using the below code. Please see if this helps. &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;dbs = spark.catalog.listDatabases()
for db in dbs:
  tables = spark.catalog.listTables(db.name)
  for t in tables:
    DDL = spark.sql("SHOW CREATE TABLE {}.{}".format(db.name, t.name))
    print(DDL.first()[0])&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2023 08:25:21 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-extract-ddl-from-tables-views-using-a-sql-query/m-p/7403#M3291</guid>
      <dc:creator>pvignesh92</dc:creator>
      <dc:date>2023-03-21T08:25:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract DDL from tables/views using a SQL query?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-extract-ddl-from-tables-views-using-a-sql-query/m-p/7404#M3292</link>
      <description>&lt;P&gt;Hi @Richard Architect​&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope all is well! Just wanted to check in if you were able to resolve your issue and would you be happy to share the solution or mark an answer as best? Else please let us know if you need more help.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;We'd love to hear from you.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Mar 2023 03:14:23 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-extract-ddl-from-tables-views-using-a-sql-query/m-p/7404#M3292</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-03-22T03:14:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract DDL from tables/views using a SQL query?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-extract-ddl-from-tables-views-using-a-sql-query/m-p/7405#M3293</link>
      <description>&lt;P&gt;Thanks but per my original post I cannot run Python in my cluster, only SQL.  Above code throws an error.&lt;/P&gt;</description>
      <pubDate>Thu, 30 Mar 2023 03:53:17 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-extract-ddl-from-tables-views-using-a-sql-query/m-p/7405#M3293</guid>
      <dc:creator>RichardSCarchit</dc:creator>
      <dc:date>2023-03-30T03:53:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract DDL from tables/views using a SQL query?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-extract-ddl-from-tables-views-using-a-sql-query/m-p/7406#M3294</link>
      <description>&lt;P&gt;Hi @Richard Architect​&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm sorry you could not find a solution to your problem in the answers provided.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Our community strives to provide helpful and accurate information, but sometimes an immediate solution may only be available for some issues.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I suggest providing more information about your problem, such as specific error messages, error logs or details about the steps you have taken. This can help our community members better understand the issue and provide more targeted solutions.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Alternatively, you can consider contacting the &lt;A href="https://help.databricks.com/s/login/?ec=302&amp;amp;startURL=%2Fs%2Fsubmitrequest%3F_ga%3D2.21602814.410387879.1677483027-1154058422.1670836178" alt="https://help.databricks.com/s/login/?ec=302&amp;amp;startURL=%2Fs%2Fsubmitrequest%3F_ga%3D2.21602814.410387879.1677483027-1154058422.1670836178" target="_blank"&gt;support team&lt;/A&gt; for your product or service. They may be able to provide additional assistance or escalate the issue to the appropriate section for further investigation.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you for your patience and understanding, and please let us know if there is anything else we can do to assist you.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Mar 2023 05:38:04 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-extract-ddl-from-tables-views-using-a-sql-query/m-p/7406#M3294</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-03-30T05:38:04Z</dc:date>
    </item>
  </channel>
</rss>

