jdbc 調用 sql server 的存儲過程時“該語句沒有返回結果集”的解決方法

在JDBC中調用SQL Server中的存儲過程時出現如下異常: 

  1. com.microsoft.sqlserver.jdbc.SQLServerException: 該語句沒有返回結果集。   

  2.         at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:171)   

  3.         at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:394)   

  4.         at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:340)   

  5.         at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)   

  6.         at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)   

  7.         at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)   

  8.         at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)   

  9.         at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:283)  

  10. 解決方法,添加下面代碼

SET NOCOUNT ON

下面是一個存儲過程案例,裏面用到了"臨時表"技術
  1. USE [xngzf]
    GO
    /****** Object:  StoredProcedure [dbo].[PROC_GzfRentMonthZj]    Script Date: 01/03/2014 16:49:50 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER proc [dbo].[PROC_GzfRentMonthZj]
    @fyXqCode varchar(50),
    @fyDhCode varchar(50),
    @affairsMonth varchar(50),
    @lastPerson varchar(50), 
    @lastGov  varchar(50)
    as
    	SET NOCOUNT ON
    	declare @htID varchar(50)
    	declare @chengzfID varchar(50)
    	declare @fyID varchar(50)
    	declare @accountID varchar(50)
    	declare @accountTotal decimal(28, 2)
    	declare @fyzjMonth decimal(28, 2)
    	
    	declare @htCode varchar(50)
    	declare @chengzfName varchar(50)
    	declare @fyName varchar(500)
    	declare @acountTotal decimal(28, 2)
    	declare @resultInfo varchar(500)
    	
    	declare @Num int = 0
    	declare @i int = 1
    
    	/**創建臨時表,返回錯誤信息**/
    	create  table  Du_ErrorInfo ( 
    	  htCode varchar(50), 
    	  chengzfName  varchar(50),
    	  fyName  varchar(500),
    	  accountID  varchar(50),
    	  accountTotal  decimal(28, 2),
    	  fyzjMonth decimal(28, 2),
    	  resultInfo  varchar(50) DEFAULT '賬戶餘額不足',
    	)
    	
    	/**獲得總數**/
    	set @Num = (select COUNT(*) from tb_xngzf_basic_ht ht
    	left join tb_xngzf_basic_fang fy on ht.fyID = fy.fyID
    	left join tb_xngzf_basic_chengzf czf on ht.chengzfID = czf.zuhuID
    	left join tb_qx_weidu xq on xq.weiduID = fy.fyXqCode	--關聯得到小區名稱
    	left join tb_qx_weidu dh on dh.weiduID = fy.fyDhCode	--關聯得到棟號名稱
    	left join tb_xngzf_basic_ht_child chd on ht.htID = chd.htID
    	where 1=1
    	and ht.isPayMonenty = 2
    	and @affairsMonth between substring(replace(chd.startDate, '-', ''), 0, 7) and substring(replace(chd.endDate, '-', ''), 0, 7)
    	and fy.fyXqCode = @fyXqCode
    	and fy.fyDhCode = @fyDhCode)
    	
    	while( @i<=@Num )
    	begin
    		/**查找htID, chengzfID, fyID**/
    		select @htID=fo.htID, @chengzfID=fo.chengzfID, @fyID=fo.fyID, @htCode=fo.htCode, @chengzfName=fo.chengzfName, @fyName=fo.fyName from(
    			select ht.htID, ht.chengzfID, ht.fyID, ROW_NUMBER() over(order by ht.htID) as rowNumber, ht.htCode, czf.zuhuName as chengzfName, xq.weiduName+dh.weiduName+fy.fyFh as fyName from tb_xngzf_basic_ht ht
    			left join tb_xngzf_basic_fang fy on ht.fyID = fy.fyID
    			left join tb_xngzf_basic_chengzf czf on ht.chengzfID = czf.zuhuID
    			left join tb_qx_weidu xq on xq.weiduID = fy.fyXqCode	--關聯得到小區名稱
    			left join tb_qx_weidu dh on dh.weiduID = fy.fyDhCode	--關聯得到棟號名稱
    			left join tb_xngzf_basic_ht_child chd on ht.htID = chd.htID
    			where 1=1
    			and ht.isPayMonenty = 2
    			and @affairsMonth between substring(replace(chd.startDate, '-', ''), 0, 7) and substring(replace(chd.endDate, '-', ''), 0, 7)
    			and fy.fyXqCode = @fyXqCode
    			and fy.fyDhCode = @fyDhCode
    		)fo
    		where fo.rowNumber = @i		
    		begin
    		/**判斷扣租表裏該月份是否已扣租**/
    		if( not exists(select * from tb_xngzf_basic_money_deduct where htID=@htID and affairsMonth=replace(@affairsMonth, '-', '')) )
    			begin 
    				/**查找accountID**/
    				if(@chengzfID is not null)
    				begin
    					select @accountID=accountID from tb_xngzf_basic_money_account where chengzfID = @chengzfID
    				end
    				set @accountTotal = (select accountTotal from tb_xngzf_basic_money_account where accountID = @accountID)
    				set @fyzjMonth = (select fyZujinMonth from tb_xngzf_basic_ht_child where htID = @htID)
    				--set @accountTotal = 50
    				--set @fyzjMonth = 100
    				if(@accountTotal>=@fyzjMonth)
    				begin
    					begin tran
    					--print @htID + ',' + @chengzfID + ',' + @fyID
    					/**更新賬戶表的賬戶金額**/
    					update tb_xngzf_basic_money_account set expensesTotal=expensesTotal+@fyzjMonth, accountTotal=accountTotal-@fyzjMonth where accountID=@accountID
    					if @@ERROR <> 0
    					begin 
    						rollback tran
    					end
    					/**插入賬戶子表一條扣費記錄**/
    					insert into tb_xngzf_basic_money_account_child(feeID, accountID, chengzfID, szType, income, expenses, accountTotal, scDesc, lastTime, lastPerson, lastGov)
    					values((select MAX(CONVERT(bigint, feeID)+1) from tb_xngzf_basic_money_account_child), @accountID, @chengzfID, 4, 0, @fyzjMonth, @accountTotal-@fyzjMonth, '日常扣租', (select convert(nvarchar,getdate(),23)), @lastPerson, @lastGov)
    					if @@ERROR <> 0
    					begin 
    						rollback tran
    					end
    					/**插入扣租表一條扣租記錄**/
    					insert into tb_xngzf_basic_money_deduct(kfID, htID, fyID, chengzfID, affairsMonth, sfMoney, sfDesc, lastTime, lastPerson, lastGov)
    					values((select MAX(CONVERT(bigint, kfID)+1) from tb_xngzf_basic_money_deduct), @htID, @fyID, @chengzfID, replace(@affairsMonth, '-', ''), @fyzjMonth, '來自扣租', (select convert(nvarchar,getdate(),23)), @lastPerson, @lastGov)
    					if @@ERROR <> 0
    					begin 
    						rollback tran
    					end
    					commit tran
    				end
    				else
    				begin	
    					--插入臨時表一條記錄
    					insert into Du_ErrorInfo(htCode, chengzfName, fyName, accountID, accountTotal, fyzjMonth, resultInfo)
    					values(@htCode, @chengzfName, @fyName, @accountID, @accountTotal, @fyzjMonth, '賬戶餘額不足')
    				end
    			end
    			set @i=@i+1
    		end
    	end
    	--查詢臨時表記錄
    	select due.htCode, due.chengzfName, due.fyName, due.accountID, due.accountTotal, due.fyzjMonth, due.resultInfo from Du_ErrorInfo due
    	--刪除臨時表
    	drop table Du_ErrorInfo
    	
    	--調存儲過程
    	exec PROC_GzfRentMonthZj  '77', '78', '2014-06', '1', ''


發佈了29 篇原創文章 · 獲贊 8 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章