Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

sql server - jdbc sql error: statement did not return a result set

I have two stored procedures as follows:

create stored procedure p1
as
    select * from table1 where datediff(day, table1.[date], getdate())

create stored procedure p2
as
   declare @t1 table(
     ref varchar(20)
   )
      insert into @t1 select * from table1 where ref = 'some ref'
      declare @t2 table(
      fname varchar(20),
      lname varchar(20),
      email varchar(1000)
  )
  declare @len int = (select count(ref) from @t1)
  while @len > 0
  begin
  declare @value varchar(20)  = (select top 1 ref from @t1)
  insert into @t2 select * from table2 where ref = @ref
  delete from @t1
  where ref = @value
  set @len = (select count(ref) from @t1)
  end
  select * from @t2

Java code

 ....
 String query = "Execute [p2]";

 try(CallableStatement cstmt = conn.prepareCall(query);
     ResultSet rs = cstmt.executeQuery()){
        ... some code
    }

The table variable @t1 hold select result from a table 'table1'

The variable @len hold the number of rows in @t1

Using @len > 0 as condition in while loop, I want to select records from another table 'table2' the table variable @t2 hold the select records from 'table2'

The delete statement removes value from @t1 @len set to new number of rows in @t1 the last statement return all the records store in @t2

The first procedure works fine, but the second procedure works only in SQL Server.

I get this an error message in my java application

statement did not return a resultset

I want this to return a result set with the select statement I have at the end of the query.

Please is there a way around this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your [p2] stored procedure needs to include SET NOCOUNT ON right at the beginning to suppress the "n rows affected" counts so JDBC doesn't get confused as to what it should put into the ResultSet:

CREATE PROCEDURE p2
AS

SET NOCOUNT ON;

declare @t1 table(
    ref varchar(20)
)

-- ... and so on

For more information on SET NOCOUNT see

SET NOCOUNT (Transact-SQL)

For more information on precisely what gets returned from a stored procedure call, see

How to get everything back from a stored procedure using JDBC


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...