参数不会传递给函数内的函数

时间:2016-01-07 22:27:47

标签: postgresql

我正在使用postgres函数来创建交叉表。

我的第一个动态查询函数(dynamic_crosstab)创建一个sql select语句,然后这个select语句在执行时给出最终结果。 dynamic_crosstab功能完美运行

我需要使用参数执行这个select查询(dynamic_crosstab函数的结果),所以我再次使用一个函数。

CREATE OR REPLACE FUNCTION primary_cross(
    date,
    date,
    integer)
  RETURNS text AS
$BODY$
declare
val_1 text;
begin
select * from dynamic_crosstab('select 
        p.location_id, p.employee_id, pt.description, sum(p.hours_allocated) as hours_allocated
    from
        preference_type pt, preference p, preference_date_etl pde, date_etl de
    where
        pt.id = p.preference_type_id and
        pde.preference_id = p.id and
        pde.corporation_id = $3 and
        de.id = pde.date_etl_id and
        pde.deleted = ''''N'''' and
        p.deleted = ''''N'''' and
        pt.deleted = ''''N'''' and
        de.local_date between ''''$1'''' and ''''$2'''' and
        p.employee_id IN (
            select id from employee where user_id IN (select id from app_user where corporation_id = $3))
    group by p.location_id, p.employee_id, pt.description',
'select distinct description from preference_type where corporation_id =$3',
'text','location_id int , employee_id int',false) INTO val_1;
return val_1;
end;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

请注意,dynamic_crosstab函数使用4个输入参数。 2个sql查询,2个文本值和一个布尔值。

现在的问题是参数是否正确传递给dynamic_crosstab函数的第一个输入,但它们没有传递给dynamic_crosstab的第二个输入,即:

'select distinct description from preference_type where deleted=''''N'''' and corporation_id =$3'

我收到以下错误:

ERROR:  there is no parameter $3
LINE 1: ...ct description from preference_type where corporation_id =$3
                                                                     ^
QUERY:  select distinct description from preference_type where corporation_id =$3

请告知我如何使用参数来完成这项工作。

谢谢。

0 个答案:

没有答案