how to update all tables with a particular column name

后端 未结 1 1977
遇见更好的自我
遇见更好的自我 2021-01-16 19:05

I am trying to update all tables starting with string like \'agg%\' and column_name =\'%userid%\'... But i dont see any such examples online even though i was able to find o

相关标签:
1条回答
  • 2021-01-16 19:19

    To get the update query for your condition

         select 
            'update '||c.table_name||' set '||c.COLUMN_NAME||' = ''rajeev'';' 
             as my_update_query
         from 
            (select 
                table_name,COLUMN_NAME
             from INFORMATION_SCHEMA.COLUMNS 
             where table_name LIKE 'agg%' and COLUMN_NAME LIKE '%userid%') c
    

    To execute

     do $$
      declare
        arow record;
      begin
        for arow in
        select 
            'update '||c.table_name||' set '||c.COLUMN_NAME||' = ''rajeev'';' 
             as my_update_query
         from 
            (select 
                table_name,COLUMN_NAME
             from INFORMATION_SCHEMA.COLUMNS 
             where table_name LIKE 'agg%' and COLUMN_NAME LIKE '%userid%') c
        loop
         execute arow.my_update_query;
        end loop;
      end;
    $$;
    
    0 讨论(0)
提交回复
热议问题