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

shan_chandra
Esteemed Contributor
Esteemed Contributor

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

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!