错误:“$func$ postgres 错误

问题描述 投票:0回答:1

我正在尝试创建该函数 -

CREATE OR REPLACE FUNCTION to_tstz_immutable(t text)
  RETURNS timestamptz
  LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE
$func$
BEGIN 
    RETURN date_trunc('hour', t);
END
$func$;

当我运行此函数时,出现以下错误 -

错误:“$func$

处或附近有语法错误”

开始

返回 date_trunc('小时', t);

结束

$func$" 第 4 行:$func$ ^

SQL状态:42601 性格:128

这个功能有什么问题吗?

postgresql
1个回答
0
投票

您的函数有几个语法问题:

  • 在美元报价之前您需要
    as
    。所以也许
    as $$function$$
  • 您不能将
    data_trunc()
    函数与日期参数的
    text
    参数一起使用。 IE。必须是
    date
    timestamp
    timestamptz
  • SQL 函数不使用/不需要
    begin ... end

调整上述功能变为:

create or replace function to_tstz_immutable(t timestamptz)
  returns timestamptz
  language sql immutable strict parallel safe
as $func$
   select date_trunc('hour', t);
$func$;
© www.soinside.com 2019 - 2024. All rights reserved.