postgres procedure giving ERROR: invalid input syntax for type timestamp: "select CURRENT_TIMESTAMP - INTERVAL '7 day'"

postgres procedure giving ERROR: invalid input syntax for type timestamp: "select CURRENT_TIMESTAMP - INTERVAL '7 day'"

我已经创建了 postgres 过程,我不会在这里写整个过程,只写下面给出错误的部分,

create or replace procedure xyz() as $$
declare
  v_time timestamp without time zone;
  v_retain_days int;
Begin
  select retain_days from abc into v_retain_days;
  v_time := cast('select CURRENT_TIMESTAMP - INTERVAL ''' || v_retain_days || ' day''' as timestamp) ;
END;

调用这个程序,报错如下:

ERROR: invalid input syntax for type timestamp: "select CURRENT_TIMESTAMP - INTERVAL '7 day'"

有人可以帮忙吗?

无需 SELECT、动态 SQL、转换或字符串连接:

v_time := CURRENT_TIMESTAMP - INTERVAL '1 day' * v_retain_days;

或者:

v_time := CURRENT_TIMESTAMP - make_interval(days => v_retain_days);