【问题标题】:postgres procedure giving ERROR: invalid input syntax for type timestamp: "select CURRENT_TIMESTAMP - INTERVAL '7 day'"postgres 过程给出错误:类型时间戳的无效输入语法:“选择 CURRENT_TIMESTAMP - INTERVAL '7 day'”
【发布时间】:2021-07-13 14:19:49
【问题描述】:

我已经创建了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;

使用此过程调用,其报错如下:

错误:时间戳类型的输入语法无效:“select CURRENT_TIMESTAMP - INTERVAL '7 day'”

有人可以帮忙吗?

【问题讨论】:

    标签: postgresql procedure


    【解决方案1】:

    不需要 SELECT、动态 SQL、强制转换或字符串连接:

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

    或者:

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

    【讨论】:

      猜你喜欢
      • 2012-12-24
      • 2020-11-13
      • 2019-02-11
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多