根据要求,编辑以提供更多详细信息
我有一个存储过程(我们称之为 spOuter
)如下:
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
当我跑
spInner
从 SSMS 它不返回任何结果集。但是,当我从
spOuter
运行它时我得到两个结果集,一个来自 spInner
一个来自 spOuter
(注释掉对 spInner
的调用会删除多余的结果集)。spInner
如下: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)
当然,这些选择是插入/更新语句的一部分,不应返回结果集?
有没有办法解决?
SQLServer 2005 SP2 版本 9.00.4035.00
最佳答案
也许在 spInner
中更改更新语句的语法使用 alias (见 Good way to use table alias in Update statement?)和 JOIN
可能会改变行为 - 像这样,例如:
UPDATE [locations]
SET Distance = CalcDistance
FROM dbo.tblMovementLocations AS [locations]
INNER JOIN vwExcessiveOrderDistances AS [vw]
ON [locations].MovementLocationId = [vw].MovementLocationId;
思考过程是
vwExcessiveOrderDistances
中可能没有任何相应的记录。匹配于 dbo.tblMovementLocations
,也许这会导致数据库引擎通过将语句视为 SELECT
来返回空结果集。关于 dbo.tblMovementLocations
的声明.如果是这样的话,肯定会很奇怪!不过这只是我的猜测……
关于sql-server - 抑制来自嵌套存储过程调用的虚假结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13831040/