[英]How can i traverse all the tables with specific name and then update a particular column value from each table
How can i traverse all the tables with specific name (let's say'%SP%') and then update a particular column value from each table in snowflake procedure?如何遍历具有特定名称的所有表(假设为“%SP%”),然后在雪花过程中更新每个表的特定列值? Also I have to display how many rows updated from each table Like- table1 - xyz Rows updated
此外,我必须显示从每个表更新的行数 Like-table1 - xyz Rows updated
You can use您可以使用
But please note: Depending on your privileges and context the results can vary.但请注意:根据您的权限和上下文,结果可能会有所不同。 SHOW has an additional parameter "in account" and INFORMATION_SCHEMA is individual per database.
SHOW 有一个附加参数“in account”,INFORMATION_SCHEMA 是每个数据库的单独参数。 ACCOUNT_USAGE.TABLES is across all databases.
ACCOUNT_USAGE.TABLES 跨所有数据库。
Unless you're going to do this repetatively, a stored procedure is more work than just using a SQL generator.除非您要重复执行此操作,否则存储过程比仅使用 SQL 生成器更有效。 You can use the
show
command as Marcel notes, or you can use the SNOWFLAKE database to get a list of all matching columns.您可以使用
show
命令作为 Marcel 的注释,也可以使用 SNOWFLAKE 数据库来获取所有匹配列的列表。 I used the SNOWFLAKE database in this example, which assumes that nobody's created a new matching column in the last several minutes (it takes some time to reflect new changes in the SNOWFLAKE database).我在本例中使用了 SNOWFLAKE 数据库,它假设在过去几分钟内没有人创建新的匹配列(需要一些时间来反映 SNOWFLAKE 数据库中的新更改)。
This query shows how to find matching columns:此查询显示如何查找匹配的列:
-- Find columns in any database matching the pattern '%SP%'. Note: The SNOWFLAKE database has information that may be up to 3 hours later than the changes.
select C.TABLE_CATALOG, C.TABLE_SCHEMA, C.TABLE_NAME, C.COLUMN_NAME
from "SNOWFLAKE"."ACCOUNT_USAGE"."COLUMNS" C
left join "SNOWFLAKE"."ACCOUNT_USAGE"."TABLES" T on C.TABLE_ID = T.TABLE_ID
where T.TABLE_TYPE = 'BASE TABLE' and C.TABLE_CATALOG not in ('SNOWFLAKE') and C.TABLE_SCHEMA not in ('INFORMATION_SCHEMA') and C.DATA_TYPE = 'TEXT' and C.COLUMN_NAME like '%SP%';
This query shows a SQL generator that will generate the UPDATE statements.此查询显示将生成 UPDATE 语句的 SQL 生成器。
select listagg(concat('update "', C.TABLE_CATALOG, '"."', C.TABLE_SCHEMA, '"."', C.TABLE_NAME, '" set "', COLUMN_NAME, '"=''NEW_VALUE'' where ', COLUMN_NAME, '=''OLD_VALUE'''), ';\n') || ';'
from "SNOWFLAKE"."ACCOUNT_USAGE"."COLUMNS" C
left join "SNOWFLAKE"."ACCOUNT_USAGE"."TABLES" T on C.TABLE_ID = T.TABLE_ID
where T.TABLE_TYPE = 'BASE TABLE' and C.TABLE_CATALOG not in ('SNOWFLAKE') and C.TABLE_SCHEMA not in ('INFORMATION_SCHEMA') and C.DATA_TYPE = 'TEXT' and C.COLUMN_NAME like '%SP%';
As far as capturing the row counts updated, that will be in the query history.至于捕获更新的行数,这将在查询历史记录中。 If this is a one-and-done thing, it's easier to do it this way.
如果这是一劳永逸的事情,那么这样做会更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.