繁体   English   中英

无法在交叉表函数 postgres 中传递参数

[英]Unable to pass arguments in crosstab function postgres

我知道我必须将文本传递给 postgres 中的交叉表函数。 但不知何故,我无法做到这一点。 我不确定我做错了什么,请帮助。 这是我要创建的功能

    create or replace function hrms.test2(startdate date)
returns table(
employeeid int,
col1 int,
col2 INT,
col3 int,
col4 int) as
$body$
SELECT * FROM hrms.crosstab(
  $firstquery$ 
  SELECT tms.employeeid,tms.today,count(tms.employeeid) as countid 
  FROM hrms.timesheet as tms
  where dated>=|| quote_literal(startdate) ||
  and dated < ||+ quote_literal(startdate)||::timestamp + '1 MONTH'::INTERVAL
  group by tms.employeeid,tms.today $firstquery$,
  $secquery$ select distinct tms.today 
  from hrms.timesheet as tms$secquery$
)as
finalresult(employeeid int,leave int,present int,absent int, holiday int)
$body$ 
LANGUAGE SQL;

它运行成功但是当我使用日期运行它时

select * from hrms.test2('2017-09-01')

我收到一条错误消息说

column startdate doesn't exist

我也尝试了更多替代方案。 我不确定我做错了什么请帮忙。

你忘了用引用$firstquery$来包装变量,从你的代码中我假设你想要像这样的东西:

create or replace function hrms.test2(startdate date)
returns table(
employeeid int,
col1 int,
col2 INT,
col3 int,
col4 int) as
$body$
SELECT * FROM hrms.crosstab(
  $firstquery$ 
  SELECT tms.employeeid,tms.today,count(tms.employeeid) as countid 
  FROM hrms.timesheet as tms
  where dated>=$firstquery$|| quote_literal(startdate) ||$firstquery$
  and dated < $firstquery$||+ quote_literal(startdate)||$firstquery$::timestamp + '1 MONTH'::INTERVAL
  group by tms.employeeid,tms.today $firstquery$,
  $secquery$ select distinct tms.today 
  from hrms.timesheet as tms$secquery$
)as
finalresult(employeeid int,leave int,present int,absent int, holiday int)
$body$ 
LANGUAGE SQL;

您基本上可以使用 $$ 而不是 $firstquery$ 或 $secondquery$。

我没有代表发表评论,所以我留下了回复。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM