2017-09-13 70 views
0

我知道我必須將文本傳遞給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 

我有嘗試了更多的替代方案。我不知道我在做什麼錯誤請幫忙。

+0

有誰能夠請回復至少 – Vikram

回答

0

你忘了換出與報價$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; 
+0

由於它的第二行移除+符號後工作的quote_nullable。但它真的很感謝很多人...... – Vikram