PostgreSQL: month := interval '30 days';

时间秒杀一切 提交于 2019-12-11 01:26:59

问题


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...


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4713490/postgresql-month-interval-30-days

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!