cancel
Showing results for 
Search instead for 
Did you mean: 
Community Discussions
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 REPLY 1

shan_chandra
Honored Contributor III
Honored Contributor III

@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.

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.