PostgreSQL: 月 := 间隔 '30 天';

发布于 2024-10-12 16:39:25 字数 871 浏览 7 评论 0原文

尝试从 2 个表中删除超过 1 个月的记录,其中 1 引用另一个表中的“id”列:

create or replace function quincytrack_clean()
        returns void as $BODY$
        begin
                month := interval '30 days';

                delete from hide_id
                where id in
                (select id from quincytrack
                where age(QDATETIME) > month);

                delete from quincytrack
                where age(QDATETIME) > month;
        end;
$BODY$ language plpgsql;

但这失败了:

ERROR:  syntax error at or near "month"
LINE 1: month := interval '30 days'
        ^
QUERY:  month := interval '30 days'
CONTEXT:  SQL statement in PL/PgSQL function "quincytrack_clean" near line 2

我正在阅读 文档,但不明白我的声明有什么问题......

Trying to delete records older than 1 month from 2 tables, where 1 references the "id" column in another:

create or replace function quincytrack_clean()
        returns void as $BODY$
        begin
                month := interval '30 days';

                delete from hide_id
                where id in
                (select id from quincytrack
                where age(QDATETIME) > month);

                delete from quincytrack
                where age(QDATETIME) > month;
        end;
$BODY$ language plpgsql;

but this fails with:

ERROR:  syntax error at or near "month"
LINE 1: month := interval '30 days'
        ^
QUERY:  month := interval '30 days'
CONTEXT:  SQL statement in PL/PgSQL function "quincytrack_clean" near line 2

I'm reading the doc, but don't understand what's wrong with my declaration...

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

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

发布评论

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

评论(2

英雄似剑 2024-10-19 16:39:25

您需要声明变量“month”,即:

declare
    month interval;
begin
    month := interval '30 days';
end;

此外,您可能需要重新检查您的“where”标准。如果 QDATETIME 是索引列,我认为它不会使用索引,而 QDATETIME QDATETIME QDATETIME QDATETIME QDATETIME QDATETIME QDATETIME (现在()-月份) 会的。

You need to declare the variable 'month', viz.:

declare
    month interval;
begin
    month := interval '30 days';
end;

Also, you might want to re-examine your "where" criteria. If QDATETIME is an indexed column, I don't think it will use the index, whereas QDATETIME < (now() - month) would.

笙痞 2024-10-19 16:39:25

您需要先声明该变量,然后才能使用它。

...
DECLARE
   month INTERVAL;
BEGIN 
   month := interval '30 days';
 ...

但我会避免使用保留字或内部函数名称的变量名称。

You need to declare the variable before you can use it.

...
DECLARE
   month INTERVAL;
BEGIN 
   month := interval '30 days';
 ...

But I would avoid using variable names that are reserved words or internal function names.

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