Postgresql Postgres-使用单个查询在多个表中更新系统列

Postgresql Postgres-使用单个查询在多个表中更新系统列,postgresql,Postgresql,我想在单个查询中更新所有表中的sysOperatorId列 update (SELECT tablename FROM pg_catalog.pg_tables WHERE (schemaname != 'pg_catalog' AND schemaname != 'information_schema') order by tablename) set sysOperatorId = 'xxxxxx-xxx-xxxxx' where sysOperatorId = 'xxx

我想在单个查询中更新所有表中的sysOperatorId列

update (SELECT
   tablename
FROM
   pg_catalog.pg_tables
WHERE
   (schemaname != 'pg_catalog' AND schemaname != 'information_schema') order by tablename) 
set sysOperatorId = 'xxxxxx-xxx-xxxxx' where sysOperatorId = 'xxxxxx-xxx-xxxxx'

上面的子查询返回用户创建的表。在每个表中,我都有一个列sysOperatorId,我想更新该列。

为此需要动态SQL

类似以下未经测试的内容:

do
$$
declare
  l_sql text;
  l_rec record;
  l_id1 text := 'xxxxxx-xxx-xxxxx';
  l_id2 text := 'xxxxxx-xxx-xxxxx';
begin
  for l_rec in select tablename 
               from pg_catalog.tables
               where schemaname not in ('pg_catalog', 'information_schema')
  loop
     l_sql := format('update %I set sysoperatorid = $1 where sysoperatorid = $2', l_rec.tablename);
     execute l_sql 
       using l_id1, l_id2;
  end loop;
end;
$$
函数使用pg_tables.tablename中的值作为目标表生成UPDATE语句。I%是标识符的占位符,将正确处理需要引用的表


然后执行此SQL字符串,将两个ID作为参数传递,而不是将它们作为常量包含在SQL字符串中

上面的子查询返回用户创建的表。在每个表中,我都有一个列sysOperatorId,我想更新该列。有什么方法可以避免for循环吗?@NarendarReddyM:没有。但问题是什么?这实际上是一条语句。我在考虑时间,我有100多个表,有些表有200000多条记录。如果需要更新100*200000行,没有解决方案会改变这一点,无论您是否找到一个没有循环的表