In Snowflake, I am trying to create a SQL script with a for loop that outputs the results into a new table based on the the data_type column
I have a table called PROFILE_TABLE_LIST that has the columns with a table name and column name, and data type as shown below:
| TABLENAME | COLUMN_NAME | DATA_TYPE |
|---|---|---|
| Table1 | PLANTS | TEXT |
| Table1 | HEIGHT | FLOAT |
| Table2 | COLOR | TEXT |
| Table2 | SMELL | TEXT |
I am currently trying to do a for loop using a cursor and perform the queries on each of the rows to profile the table based on the column types to look something like this:
| TABLENAME | COLUMN_NAME | DATA_TYPE | COUNT | MAX_LENGTH | MAX_VALUE |
|---|---|---|---|---|---|
| Table1 | PLANTS | TEXT | 10 | 82 | NULL |
| Table1 | HEIGHT | FLOAT | 10 | NULL | 58.6 |
| Table2 | COLOR | TEXT | 20 | 56 | NULL |
| Table2 | SMELL | TEXT | 20 | 23 | NULL |
Eventually, I want to run different select statements conditioned on the data_type, but at this stage, I am only focusing on the count. This is the current loop I have. However, the select statement is not getting executed properly as the table name is being passed as a string, and if I use TABLE(tablename) I receive a syntax error (I have that line commented out below:
declare
tablename string;
column_name string;
row_count integer;
table_schema string;
table_catalog string;
name string;
tmp_array ARRAY default ARRAY_CONSTRUCT();
res resultset default (select * from PROFILE_TABLE_LIST);
c1 cursor for res;
rs_output RESULTSET;
begin
for record in c1 do
tablename := record.TABLENAME;
column_name := record.column_name;
table_schema := record.table_schema;
table_catalog := record.table_catalog;
tmp_array := array_append(:tmp_array, OBJECT_CONSTRUCT('tmp_tables', record.TABLENAME, 'COUNT', (SELECT COUNT(column_name) FROM tablename)));
-- tmp_array := array_append(:tmp_array, OBJECT_CONSTRUCT('tmp_tables', record.TABLENAME, 'COUNT', (SELECT COUNT(column_name) FROM TABLE(tablename))));
end for;
rs_output := (select value:tmp_tables, value:COUNT from table(flatten(:tmp_array)));
return table(rs_output);
end;