热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Sybase存储过程返回多ResultSet-SybaseStoredprocedurereturnedMultiResultSet

Ihaveaproblem,IusingjdbcTemplateexecaSybasestoredprocedurereturningmulitResultSet,how

I have a problem, I using jdbcTemplate exec a Sybase stored procedure returning mulit ResultSet,how could I get all the resultSet?

我有一个问题,我使用jdbcTemplate exec一个Sybase存储过程返回mulit ResultSet,我如何获得所有的ResultSet ?

if object_id('p_if_pms_spyh_upload') <> null 
    drop proc p_if_pms_spyh_upload
go
create procedure p_if_pms_spyh_upload
@status          char(1)                ---  订单状态  A/D
as
begin

    declare @rq char(8) , @errcode int, @errmsg varchar(60)     

    create table #head (
        op_type   char(2) not null,                     -- 操作类型 A 表示新建  D 表示删除
        xyqrid   varchar(30) not null,                --  确认函编号
        dh        varchar(20) not null,                -- 优惠单编号 
        yhdmc   varchar(30) not null,               -- 优惠单名称
        type      varchar(18) not null,                -- 直降种类  04表示10有函              
        qsrq     char(8) not null,                           --  起始日期
        jzrq         char(8) not null,                          -- 截止日期 
        qssj     char(8) not null,                      -- 起始时间 
        jzsj      char(8) not null,                             -- 截止时间
        jsfs      varchar(8) not null,                      ---结算方式
        oano    varchar(30) not null,                   -- OA公文号
        description   varchar(200)  not null,      -- 优惠单描述
        oprrq   char(8) not null,             -- 审批删除日期
        oprsj   char(6) not null               -- 审批删除时间    
    )     
    create table #detail (
        op_type   char(2) not null,                     -- 操作类型 A 表示新建  D 表示删除
        xyqrid   varchar(30) not null,                --  确认函编号
        dh        varchar(20) not null,                -- 优惠单编号 
        gdsid    varchar(20) not null,               --  优惠商品名称
        orgid    varchar(10)  not null,               -- 销售组织编码
        qdid     char(4) not null,                      -- 渠道编码
        tjlb        varchar(20)  not null,              -- 商品组
        pp        varchar(20)   not null,              -- 品牌编码
        gys       varchar(20)   not null,              -- 供应商编码
        fmid      varchar(10)   not null,             -- 库位编码
        yhed     numeric(16,4)  not null,            -- 优惠额度
        yhsl      numeric(16,4)   not null,            -- 优惠数量
        unit       varchar(3)  not null                 -- 销售单位
    ) 

    -- 删除1个月之前已成功发送的数据
    select @rq=convert(char(8),dateadd(month,-1,getdate()),112) 
    select @errcode=0
    select @errmsg = ''

    begin tran

        delete from IV_YHD_HEAD_SPYH_TO_PMS where  processflag = 'S' and convert(char(8),lastmodified,112)  = @rq
        if @@transtate = 2 or @@transtate = 3 or @@error != 0        
        begin       
                select @errcode=-1  
            select @errmsg = '删除IV_YHD_HEAD_SPYH_TO_PMS一月之前的数据失败!'                                     
                     goto procfail        
        end  

       delete from IV_YHD_DETAIL_SPYH_TO_PMS where  processflag = 'S' and convert(char(8),lastmodified,112)  = @rq
        if @@transtate = 2 or @@transtate = 3 or @@error != 0        
        begin     
                select @errcode=-1      
           select @errmsg = '删除IV_YHD_DETAIL_SPYH_TO_PMS一月之前的数据失败!'                                     
                     goto procfail        
        end  

        insert into #head 
        select op_type, xyqrid, dh, yhdmc, type, qsrq,jzrq, qssj, jzsj, jsfs, oano, description, oprrq, oprsj
        from IV_YHD_HEAD_SPYH_TO_PMS where processflag = 'Y' and op_type = @status

        update IV_YHD_HEAD_SPYH_TO_PMS set processflag = 'R'
        from IV_YHD_HEAD_SPYH_TO_PMS a, #head b
        where a.dh = b.dh and a.processflag = 'Y' and a.op_type = @status
      if @@transtate = 2 or @@transtate = 3 or @@error != 0        
        begin   
                select @errcode=-1       
           select @errmsg = '更新IV_YHD_HEAD_SPYH_TO_PMS表数据出错!'                                     
                     goto procfail        
        end  

        insert into #detail
        select a.op_type,a.xyqrid,a.dh,a.gdsid,a.orgid,a.qdid,a.tjlb,a.pp,a.gys,a.fmid,a.yhed,a.yhsl,a.unit
        from IV_YHD_DETAIL_SPYH_TO_PMS a,#head b
        where a.dh = b.dh and a.processflag = 'Y' and  a.op_type = @status

        update IV_YHD_DETAIL_SPYH_TO_PMS set processflag = 'R'
        from IV_YHD_DETAIL_SPYH_TO_PMS a, #head b
        where a.dh = b.dh and a.processflag = 'Y' and a.op_type = @status
      if @@transtate = 2 or @@transtate = 3 or @@error != 0        
        begin   
                select @errcode=-1       
           select @errmsg = '更新IV_YHD_DETAIL_SPYH_TO_PMS表数据出错!'                                     
                     goto procfail        
        end  


    procsuccess:
         commit trans
         goto myexit

    procfail:
         rollback trans
         goto myexit

       myexit:
          select  @errcode,@errmsg  
          select  op_type, xyqrid, dh, yhdmc, type, qsrq, jzrq,qssj, jzsj, jsfs, oano, description, oprrq, oprsj from #head
          select op_type,xyqrid,dh,gdsid,orgid,qdid,tjlb,pp,gys,fmid,yhed,yhsl,unit from #detail

end
go
grant all on p_if_pms_spyh_upload to ws
go

I got you what you say and try something else. I show you the code blow:

我给你说了你说的话,然后试试别的。我给你们看一下密码:

                        ResultSet rs = null;
                        int updateCount = -1;
                        int index = 1;

                        do {
                            updateCount = cs.getUpdateCount();
                            if (updateCount != -1) {      // it means it's a updateConut
                                cs.getMoreResults();
                                System.out.println("updateCount:" + updateCount);
                                continue;
                                }

                            rs = cs.getResultSet();
                            System.out.println(rs);
                            if (rs != null) {// it means updateCount == -1 and returns a resultSet

                                if (1 == index) {
                                    index++ ;
                                    if(rs.next()) {
                                        int errCode = rs.getInt(1);
                                        String errMsg = rs.getString(2);
                                        System.out.println("errCode:" + errCode + "    errMsg:" + errMsg);
                                        if (!(Constants.PROC_ERRORCODE_SUCC == errCode)) {
                                            logger.error("call sp errror:" + errMsg);
                                        }
                                    }
                                } else if (2 == index) {
                                    index++ ;
                                    rs = cs.getResultSet();
                                    while (rs.next()) {
                                         //proc resultSet                                               
                                    }

                                } else if (3 == index) {
                                    index++ ;
                                    rs = cs.getResultSet();
                                      // proc resultSet

                                    }

                                } else {
                                    break;
                                }

                                cs.getMoreResults();
                                continue;
                                                             }
                            // it means updateCount == -1 && rs == null nothing left to return    
                        } while (!(updateCount == -1 && rs == null));

this is the debug result

这是调试结果。

updateCount:1
updateCount:1
updateCount:1
updateCount:0
updateCount:0
updateCount:1
updateCount:1
updateCount:1
updateCount:1
org.apache.commons.dbcp.DelegatingResultSet@bd4e3c
errCode:0    errMsg: 
updateCount:1
updateCount:1
updateCount:1
null

This is the correct code

这是正确的代码

                        ResultSet rs = null;
                        int updateCount = -1;
                        int index = 1;

                        do {
                            updateCount = cs.getUpdateCount();
                            if (updateCount != -1) {// it means it returns a updateCount
                                cs.getMoreResults();
                                continue;
                            }

                            rs = cs.getResultSet();
                            if (rs != null) {// it means it returns a ResultSet
                                if (1 == index) {
                                    index++;
                                    if(rs.next()) {
                                        int errCode = rs.getInt(1);
                                        String errMsg = rs.getString(2);
//                                        System.out.println("errCode:" + errCode + "    errMsg:" + errMsg);
                                        if (!(Constants.PROC_ERRORCODE_SUCC == errCode)) {
                                            logger.error("call sp execute error:" + errMsg);
                                        }
                                    }
                                    rs.close();  // it should call close() here 
                                } else if (2 == index) {

                                    index++;
                                    while (rs.next()) {
                                          //proc the second resultSet
                                    }
                                    rs.close();     // it should call close() here 
                                } else if (3 == index) {
                                    index++;
                                    while (rs.next()) {
                                        //proc the second resultSet                                         
                                    }

                                    rs.close();  // it should call close() here 
                                } else {
                                    break;
                                }

                                cs.getMoreResults();
                                continue;                                 
                            }


                        } while (!(updateCount == -1 && rs == null)); //nothing to return 

1 个解决方案

#1


1  

You keep calling the getMoreResults in a loop

在循环中调用getMoreResults。

CallableStatement cstmt;
ResultSet rs;
int i;
...
cstmt.execute();                            // Call the stored procedure 

while (cstmt.getMoreResults()){             // If we have more Results

    rs = cstmt.getResultSet();              // Get the result set

    while (rs.next()) {                     
        i = rs.getInt(1);                      
        System.out.println("Value from result set = " + i);  

    }
}

rs.close();
cstmt.close();

For your stored proc, it looks like you always return the three resultsets

对于存储的proc,看起来您总是返回三个结果集

The code should get you close, but if you want to debug it, you can do the loop like I showed you, and inside the loop, call ResultSetMetaData rsmd = rs.getMetaData(); and the rsmd object will tell you pretty much everything about that current resultset, this way you can get a true idea of what is being returned.

代码应该会使您接近,但是如果您想要调试它,您可以像我展示的那样执行循环,在循环内部,调用ResultSetMetaData rsmd = rs.getMetaData();rsmd对象会告诉你关于当前resultset的几乎所有信息,这样你就能知道返回的是什么了。

CallableStatement cstmt;
ResultSet rs;
int i;
...
cstmt.execute();                            // Call the stored procedure 

    // select  @errcode,@errmsg

    rs = cstmt.getResultSet();              // Get the result set

    while (rs.next()) {                     
        // Process the resultset   

    }

    rs.close();

    // select  op_type, xyqrid, dh, yhdmc, type, qsrq, jzrq,qssj, jzsj, jsfs, oano, description, oprrq, oprsj from #head

    rs = cstmt.getResultSet();              // Get the result set

    while (rs.next()) {                     
        // Process the resultset   

    }

    rs.close();

    // select op_type,xyqrid,dh,gdsid,orgid,qdid,tjlb,pp,gys,fmid,yhed,yhsl,unit from #detail

    rs = cstmt.getResultSet();              // Get the result set

    while (rs.next()) {                     
        // Process the resultset   

    }

    rs.close();

cstmt.close();

推荐阅读
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 本文讨论了在手机移动端如何使用HTML5和JavaScript实现视频上传并压缩视频质量,或者降低手机摄像头拍摄质量的问题。作者指出HTML5和JavaScript无法直接压缩视频,只能通过将视频传送到服务器端由后端进行压缩。对于控制相机拍摄质量,只有使用JAVA编写Android客户端才能实现压缩。此外,作者还解释了在交作业时使用zip格式压缩包导致CSS文件和图片音乐丢失的原因,并提供了解决方法。最后,作者还介绍了一个用于处理图片的类,可以实现图片剪裁处理和生成缩略图的功能。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 原文地址:https:www.cnblogs.combaoyipSpringBoot_YML.html1.在springboot中,有两种配置文件,一种 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • Java自带的观察者模式及实现方法详解
    本文介绍了Java自带的观察者模式,包括Observer和Observable对象的定义和使用方法。通过添加观察者和设置内部标志位,当被观察者中的事件发生变化时,通知观察者对象并执行相应的操作。实现观察者模式非常简单,只需继承Observable类和实现Observer接口即可。详情请参考Java官方api文档。 ... [详细]
  • 图像因存在错误而无法显示 ... [详细]
author-avatar
亲家你要干啥
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有