使用 iBatis 的 PostgreSQL 存储过程

发布于 2024-10-08 10:50:34 字数 2186 浏览 5 评论 0原文

应用参数映射时发生错误:

--- Check the newSubs-InlineParameterMap.
--- Check the statement (query failed).
--- Cause:
org.postgresql.util.PSQLException:
ERROR: wrong record type supplied in RETURN NEXT Where:
PL/pgSQL function "getnewsubs" line 34 at return next

函数详细信息如下......

CREATE OR REPLACE FUNCTION getnewsubs(timestamp without time zone, timestamp without time zone, integer)
  RETURNS SETOF record AS
$BODY$declare
v_fromdt alias for $1;
v_todt alias for $2;
v_domno alias for $3;
v_cursor refcursor;
v_rec record;
v_cpno bigint;
v_actno int;
v_actname varchar(50);
v_actid varchar(100);
v_cpntypeid varchar(100);
v_mrp double precision;
v_domname varchar(100);
v_usedt timestamp without time zone;
v_expirydt timestamp without time zone;
v_createdt timestamp without time zone;
v_ctno int;
v_phone varchar;
begin
open v_cursor for select cpno,c.actno,usedt from cpnusage c inner join account s on s.actno=c.actno where usedt >= $1 and usedt < $2 and validdomstat(s.domno,v_domno) order by c.usedt;
fetch v_cursor into v_cpno,v_actno,v_usedt;
while found
loop
if isactivation(v_cpno,v_actno,v_usedt) IS TRUE  then
select into v_actno,v_actname,v_actid,v_cpntypeid,v_mrp,v_domname,v_ctno,v_cpno,v_usedt,v_expirydt,v_createdt,v_phone a.actno,a.actname as name,a.actid as actid,c.descr as cpntypeid,l.mrp as mrp,s.domname as domname,c.ctno as ctno,b.cpno,b.usedt,b.expirydt,d.createdt,a.phone from account a
inner join cpnusage b on a.actno=b.actno
inner join cpn d on b.cpno=d.cpno
inner join cpntype c on d.ctno=c.ctno
inner join ssgdom s on a.domno=s.domno
left join price_class l ON l.price_class_id=b.price_class_id
where validdomstat(a.domno,v_domno) and b.cpno=v_cpno and b.actno=v_actno;

select into v_rec v_actno,v_actname,v_actid,v_cpntypeid,v_mrp,v_domname,v_ctno,v_cpno,v_usedt,v_expirydt,v_createdt,v_phone;
return next v_rec;
end if;

fetch v_cursor into v_cpno,v_actno,v_usedt;
end loop;
return ;
end;$BODY$
  LANGUAGE 'plpgsql' VOLATILE;
ALTER FUNCTION getnewsubs(timestamp without time zone, timestamp without time zone, integer) OWNER TO radius

如果我从控制台运行该函数,它运行良好并给出正确的响应。但是当通过java使用时就会出现上述错误。

The error occurred while applying a parameter map:

--- Check the newSubs-InlineParameterMap.
--- Check the statement (query failed).
--- Cause:
org.postgresql.util.PSQLException:
ERROR: wrong record type supplied in RETURN NEXT Where:
PL/pgSQL function "getnewsubs" line 34 at return next

the function detail is as below....

CREATE OR REPLACE FUNCTION getnewsubs(timestamp without time zone, timestamp without time zone, integer)
  RETURNS SETOF record AS
$BODY$declare
v_fromdt alias for $1;
v_todt alias for $2;
v_domno alias for $3;
v_cursor refcursor;
v_rec record;
v_cpno bigint;
v_actno int;
v_actname varchar(50);
v_actid varchar(100);
v_cpntypeid varchar(100);
v_mrp double precision;
v_domname varchar(100);
v_usedt timestamp without time zone;
v_expirydt timestamp without time zone;
v_createdt timestamp without time zone;
v_ctno int;
v_phone varchar;
begin
open v_cursor for select cpno,c.actno,usedt from cpnusage c inner join account s on s.actno=c.actno where usedt >= $1 and usedt < $2 and validdomstat(s.domno,v_domno) order by c.usedt;
fetch v_cursor into v_cpno,v_actno,v_usedt;
while found
loop
if isactivation(v_cpno,v_actno,v_usedt) IS TRUE  then
select into v_actno,v_actname,v_actid,v_cpntypeid,v_mrp,v_domname,v_ctno,v_cpno,v_usedt,v_expirydt,v_createdt,v_phone a.actno,a.actname as name,a.actid as actid,c.descr as cpntypeid,l.mrp as mrp,s.domname as domname,c.ctno as ctno,b.cpno,b.usedt,b.expirydt,d.createdt,a.phone from account a
inner join cpnusage b on a.actno=b.actno
inner join cpn d on b.cpno=d.cpno
inner join cpntype c on d.ctno=c.ctno
inner join ssgdom s on a.domno=s.domno
left join price_class l ON l.price_class_id=b.price_class_id
where validdomstat(a.domno,v_domno) and b.cpno=v_cpno and b.actno=v_actno;

select into v_rec v_actno,v_actname,v_actid,v_cpntypeid,v_mrp,v_domname,v_ctno,v_cpno,v_usedt,v_expirydt,v_createdt,v_phone;
return next v_rec;
end if;

fetch v_cursor into v_cpno,v_actno,v_usedt;
end loop;
return ;
end;$BODY$
  LANGUAGE 'plpgsql' VOLATILE;
ALTER FUNCTION getnewsubs(timestamp without time zone, timestamp without time zone, integer) OWNER TO radius

If I run the function from the console it runs fine and gives the correct response. But when used through java it causes the above error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风柔一江水 2024-10-15 10:50:35

错误的直接原因:一个或多个变量 v_cpno,v_actno,v_usedt 的数据类型与表 cpnusageaccount 中的相应列不匹配代码>.

更一般地说,您的 plpgsql 函数不必要地复杂和晦涩。可以用一小部分代码来编写。我简化为:

CREATE OR REPLACE FUNCTION getnewsubs(timestamp, timestamp, integer)
  RETURNS TABLE (
     actno      int
    ,actname    text
    ,actid      text
    ,cpntypeid  text
    ,mrp        double precision
    ,domname    text
    ,ctno       int
    ,cpno       bigint
    ,usedt      timestamp without time zone
    ,expirydt   timestamp without time zone
    ,createdt   timestamp without time zone
    ,phone      text) AS
$BODY$
DECLARE
    r   record;
BEGIN

FOR r IN
    SELECT cpno, c.actno, usedt
    FROM   cpnusage c
    JOIN   account s ON s.actno = c.actno
    WHERE  usedt >= $1
    AND    usedt <  $2
    AND    validdomstat(s.domno, $3)
    ORDER  BY c.usedt;

LOOP
    RETURN QUERY
    SELECT a.actno
        ,a.actname
        ,a.actid
        ,c.descr        --  as cpntypeid
        ,l.mrp
        ,s.domname
        ,c.ctno
        ,b.cpno
        ,b.usedt
        ,b.expirydt
        ,d.createdt
        ,a.phone
    FROM   account a
    JOIN   cpnusage b ON a.actno = b.actno
    JOIN   cpn d ON b.cpno = d.cpno
    JOIN   cpntype c ON d.ctno = c.ctno
    JOIN   ssgdom s ON a.domno = s.domno
    LEFT   JOIN price_class l ON l.price_class_id = b.price_class_id
    WHERE  validdomstat(a.domno, $3)
    AND    b.cpno = r.cpno
    AND    b.actno = r.actno;
    AND    isactivation(r.cpno,r.actno,r.usedt) IS TRUE
END LOOP;

RETURN;

END;
$BODY$
  LANGUAGE plpgsql VOLATILE;
ALTER FUNCTION getnewsubs(timestamp without time zone, timestamp without time zone, integer) OWNER TO radius

可能可以简化为单个 RETURN QUERY。但我已经把这匹死马打败得够多了。

Immediate cause of the error: The datatype of one or more these variables v_cpno,v_actno,v_usedt does not match the corresponding columns in the tables cpnusage and account.

More generally, your plpgsql function is needlessly complicated and obscure. Could be written with a fraction of the code. I simplified to:

CREATE OR REPLACE FUNCTION getnewsubs(timestamp, timestamp, integer)
  RETURNS TABLE (
     actno      int
    ,actname    text
    ,actid      text
    ,cpntypeid  text
    ,mrp        double precision
    ,domname    text
    ,ctno       int
    ,cpno       bigint
    ,usedt      timestamp without time zone
    ,expirydt   timestamp without time zone
    ,createdt   timestamp without time zone
    ,phone      text) AS
$BODY$
DECLARE
    r   record;
BEGIN

FOR r IN
    SELECT cpno, c.actno, usedt
    FROM   cpnusage c
    JOIN   account s ON s.actno = c.actno
    WHERE  usedt >= $1
    AND    usedt <  $2
    AND    validdomstat(s.domno, $3)
    ORDER  BY c.usedt;

LOOP
    RETURN QUERY
    SELECT a.actno
        ,a.actname
        ,a.actid
        ,c.descr        --  as cpntypeid
        ,l.mrp
        ,s.domname
        ,c.ctno
        ,b.cpno
        ,b.usedt
        ,b.expirydt
        ,d.createdt
        ,a.phone
    FROM   account a
    JOIN   cpnusage b ON a.actno = b.actno
    JOIN   cpn d ON b.cpno = d.cpno
    JOIN   cpntype c ON d.ctno = c.ctno
    JOIN   ssgdom s ON a.domno = s.domno
    LEFT   JOIN price_class l ON l.price_class_id = b.price_class_id
    WHERE  validdomstat(a.domno, $3)
    AND    b.cpno = r.cpno
    AND    b.actno = r.actno;
    AND    isactivation(r.cpno,r.actno,r.usedt) IS TRUE
END LOOP;

RETURN;

END;
$BODY$
  LANGUAGE plpgsql VOLATILE;
ALTER FUNCTION getnewsubs(timestamp without time zone, timestamp without time zone, integer) OWNER TO radius

Could probably be simplified to a single RETURN QUERY. But I have beaten this dead horse quite enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文