【发布时间】: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