cancel
Showing results for 
Search instead for 
Did you mean: 
Community Platform Discussions
Connect with fellow community members to discuss general topics related to the Databricks platform, industry trends, and best practices. Share experiences, ask questions, and foster collaboration within the community.
cancel
Showing results for 
Search instead for 
Did you mean: 

fetching metadata for tables in a database stored in unity catalogue

sai_sathya
New Contributor III

Hi everyone

iam trying to fetch the metadata of every columns from an table and every tables from the database under an catalogue for that iam trying to use the samples catalogue that provided by databricks and get details for tpch database that provided by databricks with sample data in it 

sai_sathya_0-1713277488517.png

as iam prettymuch new to extract informations from unity catalogue iam unable to make an query that can be used to fetch the required information in databricks sql ui so DBRX LLM suggested me to use this code to make use of it in query editor but unfortunately iam unable to identify the mistakes or iam not even sure how to improvise the code ill share the code below please someone suggest me if this approach or if there is anyother approach that can be used to generate an metadata 

 

 

 

 

 

 

 

USE samples;
DECLARE @table_name STRING;
DECLARE @column_name STRING;
DECLARE @SQL STRING;

DECLARE table_cursor CURSOR FOR
SELECT table_name FROM information_schema.tables WHERE table_schema = 'tpch';

OPEN table_cursor;

FETCH NEXT FROM table_cursor INTO @table_name;

WHILE @@FETCH_STATUS = 0
BEGIN
    DECLARE column_cursor CURSOR FOR
    SELECT column_name FROM information_schema.columns WHERE table_name = @table_name;

    OPEN column_cursor;

    FETCH NEXT FROM column_cursor INTO @column_name;

    WHILE @@FETCH_STATUS = 0
    BEGIN
        SET @SQL = 'DESCRIBE TABLE EXTENDED tpch.`' + @table_name + '`.`' + @column_name + '`';
        EXECUTE IMMEDIATE @SQL;
        FETCH NEXT FROM column_cursor INTO @column_name;
    END;

    CLOSE column_cursor;

    FETCH NEXT FROM table_cursor INTO @table_name;
END;

CLOSE table_cursor;
DEALLOCATE table_cursor;

 

 

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

shan_chandra
Databricks Employee
Databricks Employee

@sai_sathya  - you can use DESCRIBE EXTENDED command to get the metadata of the given table. Also, you can query the information_schema.columns within your UC catalog to check the column details of a given table.

View solution in original post

1 REPLY 1

shan_chandra
Databricks Employee
Databricks Employee

@sai_sathya  - you can use DESCRIBE EXTENDED command to get the metadata of the given table. Also, you can query the information_schema.columns within your UC catalog to check the column details of a given table.

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group