如何在循环数据中删除数据

发布于 2025-02-13 21:11:27 字数 988 浏览 0 评论 0原文

请有人帮助您做到这一点。 我有一个日志表,如下所示,我将在3个月内删除所有桌子。 现在,我想在采取上述步骤时添加另一个步骤以清除日志表。示例:如果&gt放置表格;然后,90天也从日志表中删除关联的行。

谢谢

  ddl_date ddl_tag id object_name
2022-07-07 16:40:06创建外国1 raj.auth
2022-02-07 17:14:33创建表6 John.plots_source
2022-03-07 17:14:33创建表7 John.plots1
2022-04-07 17:14:33创建表8 johnb.plots_pkey2
2022-05-07 17:14:33创建表9 johna.plots_address3
 
DO $$ 
  DECLARE 
    r RECORD;
BEGIN
  FOR r IN 
    (
      SELECT id, object_name from user_monitor.ddl_history WHERE ddl_date < NOW() - INTERVAL '90 days'
    ) 
  LOOP
     EXECUTE 'DROP TABLE IF EXISTS ' || r.object_name || ' CASCADE';
  END LOOP;
 delete from user_monitor.ddl_history where not exists (select tablename from pg_catalog.pg_tables b where r.object_name = b.tablename);
     commit;
END $$ ;

SQL错误[42601]:错误:查询没有结果数据的目的地
提示:如果要丢弃选择的结果,请使用persim 反而。地点:pl/pgsql函数inline_code_block line 12 at sql 语句

Please can someone help on how to do this.
I have a log table as below where i am deleting all tables beyond 3 months.
Now i want add another step to clear the log table as well when the above step has been actioned. Example: drop the table if > 90 days then delete the associated rows from the log table too.

thanks

       ddl_date           ddl_tag     ID  object_name
2022-07-07 16:40:06   CREATE FOREIGN  1   raj.auth
2022-02-07 17:14:33   CREATE TABLE    6   john.plots_source
2022-03-07 17:14:33   CREATE TABLE    7   john.plots1
2022-04-07 17:14:33   CREATE TABLE    8   johnb.plots_pkey2
2022-05-07 17:14:33   CREATE TABLE    9   johna.plots_address3
DO $ 
  DECLARE 
    r RECORD;
BEGIN
  FOR r IN 
    (
      SELECT id, object_name from user_monitor.ddl_history WHERE ddl_date < NOW() - INTERVAL '90 days'
    ) 
  LOOP
     EXECUTE 'DROP TABLE IF EXISTS ' || r.object_name || ' CASCADE';
  END LOOP;
 delete from user_monitor.ddl_history where not exists (select tablename from pg_catalog.pg_tables b where r.object_name = b.tablename);
     commit;
END $ ;

SQL Error [42601]: ERROR: query has no destination for result data
Hint: If you want to discard the results of a SELECT, use PERFORM
instead. Where: PL/pgSQL function inline_code_block line 12 at SQL
statement

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌酱 2025-02-20 21:11:27

您是在循环外引用R.Object_name。

尝试将删除语句在循环内部拉动,以便“ r”是一个有效的参考,可以在执行上下文中所有光标值之间循环。

You're referencing r.object_name outside of the LOOP.

Try pulling the DELETE statement inside the LOOP so that "r" is a valid reference that can cycle among all cursor values within the execution context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文