【问题标题】:How to implement execute command inside for loop - postgres, dbeaver如何在for循环中实现执行命令-postgres,dbeaver
【发布时间】:2020-09-04 13:15:53
【问题描述】:

我是 postgres 的新手。我需要创建一个函数,它将获取数据库中所有表的列表,其名称存储在一个表中,然后删除所有早于 x 天且具有特定 row_status 的表的记录。有些表没有 row_status 列。 当我尝试在 dbeaver 中保存书面函数时出现错误 -> ERROR: syntax error at or near "||"

create function delete_old_records1(day1 int, row_status1 character default null, row_status2 character default null)
returns void 
language plpgsql
as $$
declare 
    c all_tables1%rowtype; 
begin
    
    
            for c in select * from all_tables1 loop 
                if exists(SELECT column_name FROM  information_schema.columns 
                WHERE  table_schema = 'yard_kondor' AND  table_name =c.table_name AND  column_name = 'row_status') then 
                    execute 'delete from '||c.table_name||' where row_create_datetime>current_date-day1+1 and 
                    row_status in (coalesce(row_status1,''P''), row_status2)'; 
                else 
                    execute 'delete from '||c.table_name||' where row_create_datetime>current_date-day1+1'; 
                    raise notice 'Table '||c.table_name||' does not have row_status column'; 
                end if; 
            end loop;       
        return; 
    commit;
end; 
$$

【问题讨论】:

    标签: postgresql plpgsql dbeaver


    【解决方案1】:

    你的直接问题是这一行:

    raise notice 'Table '||c.table_name||' does not have row_status column'; 
    

    那个should be:

    raise notice 'Table % does not have row_status column', c.table_name; 
    

    但是,您的功能可能会有所改进。一般来说,强烈建议使用format() 生成动态SQL 以正确处理标识符。您也不能在函数中使用commit。如果您确实需要,请使用程序。

    create function delete_old_records1(day1 int, row_status1 character default null, row_status2 character default null)
    returns void 
    language plpgsql
    as $$
    declare 
        c all_tables1%rowtype; 
    begin
      for c in select * from all_tables1 
      loop 
        if exists (SELECT column_name FROM  information_schema.columns 
                   WHERE table_schema = 'yard_kondor' 
                     AND table_name = c.table_name 
                     AND column_name = 'row_status') then 
          execute format('delete from %I 
                          where row_create_datetime > current_date - %J + 1 
                          and row_status in (coalesce(row_status1,%L), row_status2)', c.table_name, day1, 'P'); 
        else 
          execute format('delete from %I where row_create_datetime > current_date - day1 + 1', c.table_name); 
          raise notice 'Table % does not have row_status column', c.table_name; 
        end if; 
      end loop;       
      return; 
      -- you can't commit in a function
    end; 
    $$
    

    【讨论】:

    • 现在我可以保存函数,但目前我在运行该函数时遇到了问题。我开始使用: DO $$ BEGIN PERFORM "delete_old_records1"(31,'P','N');结束$$;我使用 ALT+X 启动脚本(也尝试了 select delete_old_records1(31,'P','N');)并出现此错误:SQL 错误 [42703]: ERROR: column "day1" does not exist Where: PL/pgSQL function delete_old_records1(integer,character,character) 第 11 行在 EXECUTE 语句 SQL 语句 "SELECT "delete_old_records1"(31,'P','N')" PL/pgSQL function inline_code_block 第 2 行在 PERFORM
    【解决方案2】:

    谢谢你的回答。现在我可以保存函数了,但目前我在运行函数时遇到了问题。

    我开始使用这个函数:

    DO $$ BEGIN
        PERFORM "delete_old_records1"(31,'P','N');
    END $$;
    

    我使用 ALT+X 启动脚本(也尝试过 select delete_old_records1(31,'P','N');)并出现此错误:

    SQL 错误 [42703]:错误:列“day1”不存在 其中:PL/pgSQL 函数 delete_old_records1(integer,character,character) 第 11 行的 EXECUTE 语句 SQL 语句 "SELECT "delete_old_records1"(31,'P','N')" PL/pgSQL function inline_code_block line 2 at PERFORM

    【讨论】:

      猜你喜欢
      • 2012-06-26
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      • 2014-06-06
      • 2013-09-13
      • 2014-02-15
      • 2021-10-28
      相关资源
      最近更新 更多