以下存储过程给出了上述错误。但是,当我将每个部分作为一个简单的查询运行时,它运行正常。我没看到什么?它应该得到不同班次的部门数据。
ALTER PROCEDURE [dbo].[DataByDepartment]
(
@referenceDateTime DATETIME
,@shiftsBackward INT
,@shiftsForward INT
,@departmentID int
)AS BEGIN
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SET NOCOUNT ON;
DECLARE @referenceStartDate DATETIME;
DECLARE @referenceEndDate DATETIME;
SET @referenceDateTime=COALESCE(@referenceDateTime,CURRENT_TIMESTAMP);
SET @shiftsBackward=COALESCE(@shiftsBackward,2);
SET @shiftsForward=COALESCE(@shiftsForward,1);
SELECT
@referenceStartDate=MIN(StartDateTime)
,@referenceEndDate=MAX(EndDateTime)
FROM dbo.GetShiftsDateTime(@referenceDateTime,@shiftsBackward,@shiftsForward,2);
WITH DataByStockCode AS
(
SELECT D.StockCode
,D.EntryDate+CAST(STUFF(STUFF(STUFF(RIGHT('00000000'+CAST(D.TrnTime AS VARCHAR(10)),8),3,0,':'),6,0,':'),9,0,':') AS TIME) AS DateTime
,IM.TrnQty AS Qty
,FD.AlphaValue
FROM [dbo].[Data] AS D
INNER JOIN [dbo].[FormData] as FD on
D.StockCode=FD.KeyField
WHERE (
(D.TrnYear>=(YEAR(CURRENT_TIMESTAMP)-1))
AND
(D.TrnMonth>=1)
AND
(D.EntryDate>=CAST(@referenceStartDate AS DATE))
AND
(D.EntryDate<=CAST(@referenceEndDate AS DATE))
AND
FD.AlphaValue=@departmentID
)
)
,ByShift AS
(
SELECT
GWSFRDT.ShiftSequence
,BSC.DateTime
,BSC.StockCode
,BSC.Qty
,BSC.AlphaValue
,GWSFRDT.StartDateTime AS ShiftStart
,GWSFRDT.EndDateTime AS ShiftEnd
,GWSFRDT.ShiftPatternName
FROM DataByStockCode AS BSC
INNER JOIN dbo.GetShiftsDateTime(@referenceDateTime,@shiftsBackward,@shiftsForward,2) AS GWSFRDT
ON(
(BSC.DateTime>=GWSFRDT.StartDateTime)
AND
(BSC.DateTime<=GWSFRDT.EndDateTime)
)
)
SELECT
BBS.ShiftSequence
,BBS.DateTime
,BBS.AlphaValue
,sum(BBS.Qty) as TotalQTY
,BBS.ShiftStart
,BBS.ShiftEnd
,BBS.ShiftPatternName
Group BY ShiftSequence,DateTime,AlphaValue,Qty,ShiftStart
,ShiftEnd
,ShiftPatternName
END
答案 0 :(得分:0)
我认为错误来自这一行:
D.EntryDate+CAST(STUFF(STUFF(STUFF(RIGHT('00000000'+CAST(D.TrnTime AS VARCHAR(10)),8),3,0,':'),6,0,':'),9,0,':') AS TIME) AS DateTime
我假设EntryDate是一个日期时间。加号被解释为带有日期时间的加法,因此“+”之后的表达式被隐式转换为int,因为您无法将时间值添加到日期时间值。 尝试
DECLARE @time time = '12:00'
SELECT GETDATE() + @time
验证。
假设TrnTime是一个时间字段,只需将其转换为日期时间:
D.EntryDate+CAST(D.TrnTime AS DATETIME) AS DateTime
它会正常工作。