简体   繁体   中英

PostgreSQL: month := interval '30 days';

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM