JDBCR4 call to MSSQL Stored Procedure does not return a result set

Scott Klement's open source interface to using JDBC database drivers from RPG. http://www.scottklement.com/jdbc/
Post Reply
msddcb
Posts: 21
Joined: Wed Jul 28, 2021 5:12 am

JDBCR4 call to MSSQL Stored Procedure does not return a result set

Post by msddcb »

We have been using JDBCR4 to run SQL statements and Stored Procedures on a
MS SQL server for a long time.

Client has moved to a new cloud based server and a few stored procedures
are not returning anything. This is a later version of MS SQL Server

In debug I can see the result of

isResultSet = JDBC_execCall( prepStmt )

Is 0

So I added a call to get the update count as

if isresultSet = *OFF;
rc = JDBC_getUpdateCount( prepstmt );
endif;

This returns a value of 16817. This does match the number of records
written to the temporary tables before the SP runs the query that should
return the result set.

So the JDBC call is interpreting the result as an update count and not a
result set.

I tried adding IsResultSet = JDBC_getMoreResults( prepstmt ); checking
the possibility of more than one result set being returned but again 0 is
returned.

What I am not understanding is how to code the stored procedure that used
temp tables to return the result set.

The SP has worked previously but that was on an older version of MS SQL
server and when the server was locally in house and not in the cloud.

If I run the query manually within MS SQL Management Studio it returns the
correct list.

We have the following JDBC jar files installed

sqljdbc.jar
sqljdbc4.jar

Not sure how to tell the actual version ?

Here is the SQL that we are getting the update count back from where we actually want the result set.

Code: Select all

declare @StartDate		datetime = '2023-07-01'
declare @EndDate		datetime = '2023-08-01'
declare @OperatorID		int      = 82
	
create table	#jobs (
				CarNumber	varchar(50),
				Fare		money,
				Tolls		money
				)

create table	#shifts (
				CarNumber		varchar(50),
				LogonTime		datetime,
				LogoffTime		datetime,
				[Minutes]		  	int,
				Kilometres		int,
				Offered			int,
				Accepted			int,
				Rejected			int,
				Recalled			int,
				Completed		int
				)


insert into		#jobs

select			VH.CarNumber												as [CarNumber],
				TD.ChargesPrice + TD.ChargesFlagfall + TD.ChargesExtras + TD.Tolls
					- TD.Discount											as [Fare],
				TD.Tolls													as [Tolls]

from			TaxiHistory.dbo.VehicleJobHistory									VJ with (nolock)
				inner join TaxiHistory.dbo.tblDispatch							TD with (nolock) on VJ.BookingID = TD.BookingID
				inner join TaxiHistory.dbo.Vehicle								VH with (nolock) on VJ.VehicleID = VH.VehicleID
				inner join (select distinct VehicleID, StartDate
						from TaxiHistory.dbo.VehicleOwnerOperatorHistory 
						where OwnerOperatorID = @OperatorID
						and OwnerOperatorTypeID = 2
						and StartDate < getdate()
						and (EndDate is null or EndDate > getdate())) as			OH	on VJ.VehicleID = OH.VehicleID 
																		and 	VJ.TimeCarCompletedJob > OH.StartDate

where			VJ.TimeCarCompletedJob between @StartDate and @EndDate
				and TD.StatusID = 7


insert into		#shifts

select			VH.CarNumber												as [Car Number],
				DL.LogonTime												as [Logged On], 
				DL.LogoffTime												as [Logged Off],
				datediff(minute,DL.LogonTime,DL.LogoffTime)						as [Minutes],
				isnull(DL.KMsAvailablePlotted + DL.KMsNotAvailable + 
				DL.KMSOnWayToJobs,0)										as [Kilometres],
				isnull(DL.JobsOffered,0)										as [Jobs Offered],
				isnull(DL.JobsAccepted,0)										as [Jobs Accepted],
				isnull(DL.JobsRejected,0)										as [Jobs Rejected],
				isnull(DL.JobsRecalled,0)										as [Jobs Recalled],
				isnull(DL.JobsCompleted,0)									as [Jobs Completed]
from			TaxiHistory.dbo.DriverLogon										DL with (nolock)
				inner join TaxiHistory.dbo.Vehicle								VH with (nolock) on VH.VehicleID = DL.VehicleID
				inner join (select distinct VehicleID, StartDate
						from TaxiHistory.dbo.VehicleOwnerOperatorHistory 
						where OwnerOperatorID = @OperatorID
						and OwnerOperatorTypeID = 2
						and StartDate < getdate()
						and (EndDate is null or EndDate > getdate())) as			OH  on DL.VehicleID = OH.VehicleID and 
																		DL.LogonTime > OH.StartDate

where			DL.LogonTime between @StartDate and @EndDate


select			SH.[Car Number],
				SH.[Hours],
				SH.[Kilometres],
				SH.[Jobs Offered],
				SH.[Jobs Accepted],
				SH.[Jobs Rejected],
				SH.[Jobs Recalled],
				SH.[Jobs Completed],
				JB.[Total Fares],
				JB.[Total Tolls]

from			(select	S.CarNumber											as [Car Number],
				isnull(sum(S.[Minutes])/60,0)									as [Hours],
				isnull(sum(S.Kilometres),0)									as [Kilometres],
				isnull(sum(S.Offered),0)										as [Jobs Offered],
				isnull(sum(S.Accepted),0)										as [Jobs Accepted],
				isnull(sum(S.Rejected),0)										as [Jobs Rejected],
				isnull(sum(S.Recalled),0)										as [Jobs Recalled],
				isnull(sum(S.Completed),0)									as [Jobs Completed]

from			#shifts S

group by		S.CarNumber) as SH inner join (select
			J.CarNumber													as [Car Number],
				sum(J.Fare)												as [Total Fares],
				sum(J.Tolls)												as [Total Tolls]

from			#jobs J

group by		J.CarNumber ) as JB on SH.[Car Number] = JB.[Car Number]
				
order by		SH.[Car Number]


drop table	#jobs
drop table	#shifts
Any suggestions gratefully appreciated.

Thanks

Don
Scott Klement
Site Admin
Posts: 796
Joined: Sun Jul 04, 2021 5:12 am

Re: JDBCR4 call to MSSQL Stored Procedure does not return a result set

Post by Scott Klement »

I know almost nothing about SQL Server... never really worked with Microsoft databases.

I believe sqljdbc.jar is the original JDBC driver, compatible with very old versions of Java. sqljdbc4.jar is the upgraded version for working with JDBC 4.0 specification. The fact that you list both of these frightens me a bit -- I have no idea which one it will use. Seems to me that you should have one or the other, not both!

Microsoft's list of the different drivers and support for them is here:
https://learn.microsoft.com/en-us/sql/c ... ort-matrix

If I'm reading it correctly, support for sqljdbc4.jar was stopped in 2017, and the newest SQL server it was compatible with was SQL Server 2016.

I did not know any of this before seeing your message, all was determined in a few minutes of searching the internet. I have no actual experience with it.
Post Reply