我的查询是否正确如何在 Postgres SQL 存储过程函数中传递参数

Is my query correct how to pass parameter in Postgres SQL stored procedure functions

我正在努力研究 如何将参数传递给 Postgres sql 中的存储过程函数。我的查询是否正确?我收到错误 是类型日期的无效输入语法:"Firstdate"。我错过了什么?请多多指教

CREATE OR REPLACE FUNCTION prisonparam(dvn_cd text,
       phc_cd  text,
       Firstdate Date,
       Seconddate Date,
       To Date) 
     RETURNS table (round text,sc bigint,scupto bigint)
     AS $$
     /* query start */
    WITH a
   AS (
       SELECT
        round AS round
       ,Sum(ben_sc) AS SC

       FROM prison_issue
       WHERE (
        DATE BETWEEN 'Firstdate'
            AND 'To'
             )
       AND dvn_cd = 'dvn_cd'
       AND phc_cd = 'phc_cd'

       GROUP BY round ORDER BY round
       )
       ,b
      AS (
     SELECT round AS round_up
     ,Sum(ben_sc) AS SC_up


     FROM prison_issue
     WHERE (
        DATE BETWEEN 'Seconddate'
            AND 'To'
           )
    AND dvn_cd = 'dvn_cd'
    AND phc_cd = 'phc_cd'
    GROUP BY round ORDER BY round
         )
   SELECT b.round_up AS round
  ,coalesce(a.sc, 0) AS SC
  ,coalesce(b.sc_up, 0) AS SCUPTO

  FROM a
  RIGHT JOIN b ON a.round = b.round_up
  /* query end*/
  $$ LANGUAGE sql;

不要双引号参数。

 DATE BETWEEN 'Firstdate'
        AND 'To'

正在尝试将 'Firstdate''To' 用作 文字

不要引用它们。

 DATE BETWEEN Firstdate
        AND To

或者,如果您必须使用 To 等可能有问题的标识符作为变量,则应将它们加双引号。但是如果你这样做,那么到处,包括参数声明列表。

 DATE BETWEEN Firstdate
        AND "To"