Showing results for 
Search instead for 
Did you mean: 

How to extract DDL from tables/views using a SQL query?

New Contributor III

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.

For each object I want to show the DDL code, and I know I can get it by executing

show create table [tablename]

but this command only runs as standalone, not as subquery within DML.

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

SELECT DBMS_METADATA.get_ddl etc. etc.

Can't use Python for a number of reasons. Thanks for any suggestion!


Honored Contributor

@Richard Architect​ 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.

But the table DDL can be obtained programmatically by using the below code. Please see if this helps.

dbs = spark.catalog.listDatabases()
for db in dbs:
  tables = spark.catalog.listTables(
  for t in tables:
    DDL = spark.sql("SHOW CREATE TABLE {}.{}".format(,

Not applicable

Hi @Richard Architect​ 

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. 

We'd love to hear from you.


New Contributor III

Thanks but per my original post I cannot run Python in my cluster, only SQL. Above code throws an error.

Not applicable

Hi @Richard Architect​ 

I'm sorry you could not find a solution to your problem in the answers provided.

Our community strives to provide helpful and accurate information, but sometimes an immediate solution may only be available for some issues.

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.

Alternatively, you can consider contacting the support team for your product or service. They may be able to provide additional assistance or escalate the issue to the appropriate section for further investigation.

Thank you for your patience and understanding, and please let us know if there is anything else we can do to assist you.

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.