日期:2014-05-18  浏览次数:20426 次

JDBC调用含有复制表语句的存储过程无返回值问题
存储过程如下:
begin
declare @i int
  declare @MeterCode varchar(50)
declare @MeterCount int
declare @sql varchar(1000)

select distinct Id = identity(int,1,1),MeterCode into #tmp from MeterTypeParameter
where Used = 1

select @MeterCount = count(Id) from #tmp

set @sql = 'create table tmp2(MeterType varchar(50),MeterAmount int)'
execute (@sql)

set @i = 1 
while (@i <= @MeterCount)
begin
select @MeterCode = MeterCode from #tmp where Id = @i
set @sql = 'insert into tmp2 select ' + '''' + @MeterCode + '''' + ',count(address) from ' + @MeterCode + 'info' + ' where UserId = ' + @accountId
execute(@sql)
set @i = @i + 1 
end
select distinct MeterType,MeterAmount from tmp2
drop table #tmp
drop table tmp2
end



JDBC调用:
public JSONArray doInPreparedStatement(PreparedStatement ps)
throws SQLException, DataAccessException {
JSONArray resultsMap = new JSONArray();
ResultSet rs = ps.getResultSet();
if (rs != null) {
while (rs.next()) {
WarningManageData wmd = new WarningManageData();
wmd.setMeterType(rs.getString("meterType"));
wmd.setMeterAmount(rs.getString("meterAmount"));
resultsMap.add(wmd);
}
} else {
while (ps.getMoreResults()) {
rs = ps.getResultSet();
if (rs != null) {
while (rs.next()) {
JSONObject wmd = new JSONObject();
wmd.element("meterType", rs.getString("meterType"));
wmd.element("meterAmount", rs.getString("meterAmount"));
resultsMap.add(wmd);
}
break;
}
}
}
return resultsMap;
}
存储过程在数据库中exec执行没有问题,可以正确返回值。可在java后台调用就无法获取返回值,查了下好像存储过程带有带有select into或者insert into select的复制表语句就会出这个问题。想请教下这个问题怎样解决,现在是必须调用存储过程。

------解决方案--------------------
存储过程里加

set nocount on