如何在postgresql中创建一个返回SETOF类型的函数?

时间:2016-10-13 20:48:25

标签: sql postgresql postgresql-9.5

如何在postgresql中创建一个返回SETOF类型的函数?我所有的尝试都给我带来了以下错误:

  

SQL函数无法返回shell类型...

下面是我试图运行的查询:

CREATE OR REPLACE FUNCTION public.assignment_scores(
    skuunit_groupset_uid text,
    userrole_uid text,
    periodid integer,
    dataset_uid text)
  RETURNS SETOF score_in_assignment AS
$BODY$

    SELECT DISTINCT sku.uid stu_uid,
    EXTRACT(DAY FROM cdsr.date - p.enddate)::int AS delay,
    skudistrict.name AS skuunit_parent_name,
    sku.name AS skuunit_group_name,
    sku.uid AS oskuunit_group_uid,
    cdsr.storedby AS username,
    ui.phonenumber
    FROM completedatasetregistration cdsr
    INNER JOIN period p ON cdsr.periodid = p.periodid
    INNER JOIN dataset ds ON cdsr.datasetid = ds.datasetid
    INNER JOIN skuunit skuvillage ON cdsr.sourceid = skuvillage.skuunitid
    INNER JOIN _skuunitstructure ous ON cdsr.sourceid = ous.skuunitid
    INNER JOIN organisationunit oudistrict ON oudistrict.organisationunitid =           `   ous.idlevel3`
    INNER JOIN skugroupmembers ougm ON ougm.skuunitid = oudistrict.skuunitid
    INNER JOIN orgunitgroup oug ON oug.orgunitgroupid = ougm.skuunitgroupid 
    INNER JOIN skuunitgroupsetmembers ougsm ON ougsm.skuunitgroupid = `   `     oug.orgunitgroupid
    INNER JOIN orgunitgroupset ougs ON ougs.orgunitgroupsetid = `         ougsm.orgunitgroupsetid` 
    AND ougs.uid = $1
    INNER JOIN users u ON cdsr.storedby = u.username
    INNER JOIN userinfo ui ON u.userid = ui.userinfoid
    INNER JOIN userrolemembers urm ON u.userid = urm.userid
    INNER JOIN userrole ur ON urm.userroleid = ur.userroleid 
    AND ur.uid = $2 
    WHERE p.periodid = $3 --lock to a single period
    AND ds.uid = $4 --lock to a single dataset
    AND EXTRACT(DAY FROM cdsr.date - p.enddate) < DATE_PART('days',    `  DATE_TRUNC('month', p.startdate) + '1 MONTH'::INTERVAL - DATE_TRUNC('month', p.startdate)) AND EXTRACT(DAY FROM cdsr.date - p.enddate) >= 0 ;

$BODY$
  LANGUAGE sql VOLATILE

我不知道是否已经提出并解决了类似的问题。如果有,请指出我。

0 个答案:

没有答案