Postgres例程声明变量语法错误

时间:2018-02-22 07:03:35

标签: sql postgresql stored-procedures

我试图使用以下代码获取postgres例程来返回表:

CREATE FUNCTION get_duration(duration INTEGER)
  RETURNS TABLE(id integer, name varchar, full_duration NUMERIC)
AS $$

DECLARE @lower_limit NUMERIC(10) = 28;
DECLARE @upper_limit NUMERIC(10) = 37.5;

BEGIN

IF duration = 45 THEN
  SET @lower_limit = 37.6;
  SET @upper_limit = 52.5;
END IF;

SELECT id, name, full_duration FROM duration WHERE up_limit = @upper_limit AND low_limit = @lower_limit;

END;
$$
LANGUAGE sql;

问题是我一直在或接近" @"语法错误。在DECLARE专线。

1 个答案:

答案 0 :(得分:1)

您需要将冒号放在DECLARE中,如

DECLARE @lower_limit NUMERIC(10) := 28;

这有效

CREATE OR REPLACE FUNCTION get_duration(duration INTEGER)
 RETURNS  TABLE(id integer, name varchar, full_duration NUMERIC) AS
 $$
DECLARE
   lower_limit NUMERIC := 28;
    upper_limit NUMERIC := 37.5;
BEGIN
       IF duration = 45 THEN
      SET lower_limit = 37.6;
      SET upper_limit = 52.5;
    END IF;

    SELECT id, name, full_duration FROM duration WHERE up_limit = upper_limit AND low_limit = lower_limit;
END
$$  LANGUAGE plpgsql;