Column pattern extraction in Snowflake

Extract all columns from all dbs that match a pattern in Snowflake
 avatar
user_7960660
sql
a year ago
1.1 kB
6
Indexable
with raw_code as 
(
    -- Get all the database names in Snowflake account (you might not have access to all of them)
    select 
        'select table_catalog, table_schema, table_name, column_name from ' || database_name || '.information_schema.columns union all' as code_to_run,
        ROW_NUMBER() OVER (ORDER BY database_name) as row_num
    from snowflake.information_schema.databases
),
-- Next we have to remove the "union all" words from the last query.
-- You could manually remove that when copying but it's mor efun to automate it
raw_code_max_line as 
(
    select max(row_num) as max_row_num from raw_code
),
code_without_last_union as 
(
    select 
        case 
            when row_num = max_row_num then replace(code_to_run, 'union all', '')
            else code_to_run
        end as code_to_run
    from raw_code
    cross join raw_code_max_line
)
-- Copy the reults of the query
select code_to_run from code_without_last_union
;


with all_columns_in_db as 
(
    --- Paste here the output of the previous query
)
select * from all_columns_in_db 
where column_name ilike '%cust%'
;
Editor is loading...
Leave a Comment