简体   繁体   English

如何遍历具有特定名称的所有表,然后更新每个表中的特定列值

[英]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您可以使用

  1. SHOW TABLES LIKE '%SP%': https://docs.snowflake.com/en/sql-reference/sql/show-tables.html显示类似于“%SP%”的表格: https : //docs.snowflake.com/en/sql-reference/sql/show-tables.html
  2. The information schema or account usage tables-view: https://docs.snowflake.com/en/sql-reference/info-schema/tables.html信息架构或账户使用表-视图: https : //docs.snowflake.com/en/sql-reference/info-schema/tables.html

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.

相关问题 如何使用特定列名更新所有表 - how to update all tables with a particular column name 更新表的多行,该表的特定列中的特定值等于从其他两个表中计算出的值 - Update multiple rows of a table having specific value in particular column equal to value calculated from two other tables 如何使用特定值更新两个特定列的记录 - How can I update a record of two specific column with a particular value 我在多个表中有相同的列,并希望将所有表中的列更新为特定值。我怎样才能做到这一点? - I have the same column in multiple tables, and want to update that column in all tables to a specific value. How can I do this? 如何从oracle架构中包含该列名的所有表表中获取特定列的最大值? - How to get the max value for a specific column from all tables tables which contains that column name in an oracle schema? select 列具有特定值的所有表中的表名 - select table name from all tables where column has specific value 如何使用特定表中的列作为表名,我需要从中删除或插入或更新我的数据 - How to use a Column in a particular table as Table name from which i need to delete or insert or update my data 如何为数据库中的所有表更新列值的特定字符 - How to update a specific characters of column value for all tables in a database 使用所有表中的新数据自动更新特定列的特定数据 - Automatically update particular data of a specific column with new data in all tables 如何用2个表中2列之间的较低值更新表中的一列(多行)? - How can I update a column (multiple rows) in a table with the lower value between 2 column in 2 tables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM