Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress bogus result set from nested stored procedure call

Tags:

sql-server

As requested, edited to provide more details

I have a stored procedure (lets call it spOuter) along the following lines:

ALTER PROCEDURE [dbo].[spOuter]
(@SelFromDateUTC smalldatetime  
,@SelToDateUTC smalldatetime  
,@SelDriverId int = null
) AS
DECLARE @SelDriverName varchar(40)
Set Nocount on

exec dbo.spInner --<<<<<<<<<<<<<<<<<<<<<<<<<<<

Select @SelDriverName = DriverName
  From dbo.tblDrivers
 Where DriverID = @SelDriverId

Set Nocount off  

Select @SelToDateUTC as @SelShiftDateUTC
      ,@SelDriverName  as SelDriverName
      , *
  From dbo.vwRptDriverProgress
 Where ActionTimeUTC between @SelFromDateUTC and @SelToDateUTC
   and DriverId = coalesce(@SelDriverId, DriverId)
 Order by DriverName,ActionTimeUTC,DriverLogId

When I run spInner from SSMS it does not return any result sets. However, when I run it from spOuter I get two result sets, one from spInner and one from spOuter (commenting out the call to spInner removes the excess result set).

spInner is as follows:

ALTER PROCEDURE [dbo].[spInner] AS
set nocount on
Declare @CutoffDate smalldatetime
Set @CutoffDate = DateAdd(Month, -1, getUTCDate()) -- no need to keep reprocessing the whole table.
if @CutoffDate < '11/1/2008'
    Set @CutoffDate = '11/1/2008'

Insert dbo.tblADIShifts  (PU.DriverId, PU.PunchInTimeUTC, PU.PunchInLocationId)
Select DriverId, PunchInTimeUTC, PunchInLocationId 
  From vwUpdDriverLogsAddShifts PU -- Distinct Driver,PunchInTimeUTC combinations.
 Where PU.PunchInTimeUTC > @CutoffDate
   and not exists (Select *
                     from dbo.tblADIShifts SH
                    Where SH.DriverId=PU.DriverId
                      and SH.PunchInTimeUTc = PU.PunchInTimeUtC)
 Order By PU.DriverId, PU.PunchInTimeUTC

Update dbo.tblADIShifts Set
       PunchOutTimeUTC = PU.PunchOutTimeUTC
      ,PunchOutLocationId = PU.PunchOutLocationId
  From vwUpdDriverLogsNewPunchOuts PU
 Where dbo.tblADIShifts.ShiftId = PU.ShiftId and PU.PunchInTimeUTC  > @CutoffDate

Insert dbo.tblDriverLogs (DriverId, ActionId, ActionTimeUTC, ShiftId, LocationId)
Select DriverId, ActionId, PunchInTimeUTC, ShiftId, PunchInLocationId
  From dbo.vwUPDDriverLogsSP

Insert dbo.tblDriverLogs (DriverId, ActionId, ActionTimeUTC, ShiftId, LocationId)
Select DriverId, ActionId, PunchOutTimeUTC, ShiftId, PunchOutLocationId
  From dbo.vwUPDDriverLogsFP

Update dbo.tblDriverLogs Set
       ShiftId = SH.ShiftId
  From dbo.vwUpdDriverLogsAssignShifts SH 
 Where SH.PunchInTimeUTC > @CutoffDate
   and dbo.tblDriverLogs.DriverLogId = SH.DriverLogId

Update dbo.tblDriverLogs Set
       ShiftId = PrevShiftId
  From dbo.vwUpdDriverLogsShiftless3 SH
 Where dbo.tblDriverLogs.DriverLogId = SH.DriverLogId

--<<<<<<<<< The bogus (and empty) result set has the columns
--<<<<<<<<< of tblMovementLocations which is only referenced here:
Update dbo.tblMovementLocations Set
       Distance = CalcDistance
  From vwExcessiveOrderDistances vw
 Where dbo.tblMovementLocations.MovementLocationId = vw.MovementLocationId

Update dbo.tblDriverLogs Set
       DriveTimeMinutes = VW.DriveTimeMinutes
      ,BreakTimeMinutes = VW.BreakTimeMinutes
      ,DelayTimeMinutes = VW.DelayTimeMinutes
      ,LocationId = VW.LocationId
  From dbo.vwUpdDriverLogs VW
 Where dbo.tblDriverLogs.DriverLogId = VW.DriverLogId
   and VW.ActionTimeUTC > @CutoffDate
   and (dbo.tblDriverLogs.DriveTimeMinutes <> VW.DriveTimeMinutes
    or dbo.tblDriverLogs.BreakTimeMinutes <> VW.BreakTimeMinutes
    or dbo.tblDriverLogs.DelayTimeMinutes <> VW.DelayTimeMinutes
    or coalesce(dbo.tblDriverLogs.LocationId,-1) <> VW.LocationId)

Surely, these selects are part of the insert/update statements and should not return result sets? Is there any way around this?

SQLServer 2005 SP2 Version 9.00.4035.00

like image 556
Peter Wilson Avatar asked Nov 13 '22 15:11

Peter Wilson


1 Answers

Perhaps changing the syntax of your update statement(s) in spInner to use an alias (see Good way to use table alias in Update statement?) and a JOIN might change the behavior - something like this, for example:

UPDATE [locations]
   SET Distance = CalcDistance
  FROM dbo.tblMovementLocations AS [locations]
 INNER JOIN vwExcessiveOrderDistances AS [vw]
         ON [locations].MovementLocationId = [vw].MovementLocationId;

The thought process being that possibly there aren't any corresponding record(s) in vwExcessiveOrderDistances to be matched with in dbo.tblMovementLocations, and maybe this is causing the database engine to return an empty result set by treating the statement like a SELECT statement on dbo.tblMovementLocations. It would certainly be strange if this is the case!

This is just my speculation though...

like image 67
Alexander Avatar answered Nov 15 '22 06:11

Alexander