Postgres游标

时间:2016-11-13 08:04:14

标签: postgresql

我创建了复合类型

Create Type TestDetailReportType1 As
(
sName text,
cDetailsTimeStamp  timestamp,
number text,
dropdi text,
queue text,
agent text,
status int,
reference int
)

我创建了一个游标,我希望返回一个复合类型列表...但是当我从TestdetailsCursortest11执行select *时,没有返回任何记录(" abc") 但直接执行时在函数内写的查询返回31行...我是postgress的新手所以我无法理解在制作此函数时哪个地方出错了,非常感谢前面的任何指导。

注意 - >我特意想在这个场景中编写一个游标......当函数返回表时,我成功地获得了结果。

CREATE OR REPLACE FUNCTION public.TestdetailsCursortest11(
    hgname text)
    RETURNS SETOF TestDetailReportType1
    LANGUAGE 'plpgsql'
AS $TestdetailsCursortest11$

DECLARE
   cDetailcursor refcursor;
   cDetailtEvent  RECORD;     -- variable to store agent event.
   cDetail callDetailReportType1;
BEGIN
    OPEN cDetailcursor FOR
    select tblUsers.UserName,tblCallEvent.StateCreateDate,tblCallRegister.Cli,tblCallRegister.DDI,tblhuntGroup.name,
    tblUsers.Extension,
    tblCallEvent.StateID,
    tblCallRegister.CallID
  from tblCallRegister
    inner join tblCallEvent on tblCallRegister.callregisterid= tblCallEvent.callregisterid 
    inner join tblUsers on tblUsers.userid=tblCallEvent.agentid 
    inner join tblhuntGroup on tblhuntGroup.HGID=tblCallEvent.HGID 
  where name=hgname;
FETCH NEXT FROM callDetailcursor INTO callDetailtEvent;
  callDetail.sName=callDetailtEvent.UserName;
  callDetail.cDetailsTimeStamp=callDetailtEvent.StateCreateDate;
  callDetail.number =callDetailtEvent.Cli;
IF callDetailtEvent.StateID = 19
THEN
  callDetail.dropdi=callDetailtEvent.DDI;
ELSE
  callDetail.dropdi=callDetailtEvent.DDI+1;
END IF;
  callDetail.queue=callDetailtEvent.name;
  callDetail.agent=callDetailtEvent.Extension;
  callDetail.status =callDetailtEvent.StateID;
  callDetail.reference=callDetailtEvent.CallID;
RETURN;
CLOSE callDetailcursor;

END;
$TestdetailsCursortest11$;

1 个答案:

答案 0 :(得分:1)

集返回函数(a.k.a。表函数)中,使用RETURN 而不是返回结果,但退出函数。

您使用RETURN NEXT <value>;返回结果行。所以你的功能看起来应该类似于:

DECLARE
   cDetail callDetailReportType1;
   cDetailtEvent RECORD;
BEGIN
   FOR cDetailtEvent IN
      SELECT ...
   LOOP
      cDetail.field1 := ...;
      cDetail.field2 := ...;

      /* return the next result row */
      RETURN NEXT cDetail;
   END LOOP;

   /*
    * This is optional; dropping out from the end
    * of a function is an implicit RETURN
    */
   RETURN;
END;

你的函数编写方式会因为没有RETURN NEXT <value>;而返回空结果。