我有一个问题,我不知道它是否与我的存储过程有关,还是与针对该存储过程中的输出数据的类似SSRS的报告有关。当我运行此存储过程时,我会看到期望的输出。我注意到它返回和整数。那值得关注吗?另外,当我尝试报告此数据的输出并使用FIRST以外的任何聚合时,我无法使任何聚合起作用。
我在做什么错或者存储过程正确吗?
USE [SPCPRD]
GO
/****** Object: StoredProcedure [dbo].[spReportPlantLineWeightEvaluation_Gainseeker] Script Date: 1/3/2019 8:26:19 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spReportPlantLineWeightEvaluation_Gainseeker]
(
@PlantCode char(30), -- '1002'
@MachineLineID char(30), -- 'ND'
@StartDate char(30), -- = '08/05/2018 00:00:00';
@EndDate char(30), -- = '09/05/2018 23:59:59';
@ProductCode char(30) -- = '20J16HD'
)
AS
BEGIN
-------------------------------------------------------------------------------------------
---- Create a CTE to hold the analysis data
-------------------------------------------------------------------------------------------
CREATE TABLE #AnalysisData
(
PLANT_CODE CHAR(30),
ITEM_CODE varchar(30),
SampleTime char(30),
MachineID char(30),
RowNum char(30),
RowPos char(30),
Cavity char(30),
TestedBy char(30),
[WEIGHT] float,
AverageLineWeight DECIMAL(10,2),
[THICKNESS SIDEWALL MIN] float,
[THICKNESS SIDEWALL MAX] float,
[OFF CENTER TOLERANCE] float,
[THICKNESS BOTTOM] float,
);
WITH FirstData AS
(
SELECT * FROM
(
SELECT OC_VDATA.UDL1 AS PLANT_CODE, dbo.fnItemCodeFromPartNo(LEFT(OC_VDAT_AUX.PARTNOAUX, 12)) AS ITEM_CODE,
OC_VDATA.UDL5 AS TestName, OC_VDAT_AUX.UDL40 AS SampleTime, OC_VDAT_AUX.UDL8 AS MachineID, OC_VDAT_AUX.UDL13 AS RowNum,
OC_VDAT_AUX.UDL21 AS RowPos, OC_VDAT_AUX.UDL12 AS Cavity, OC_VDAT_AUX.UDL18 AS TestedBy, OC_VDATA.DATA1
FROM OC_VDAT_AUX INNER JOIN
OC_VDATA ON OC_VDAT_AUX.PARTNOAUX = OC_VDATA.PARTNO AND OC_VDAT_AUX.DATETIMEAUX = OC_VDATA.DATETIME
WHERE (OC_VDATA.UDL1 = @PlantCode) AND (OC_VDATA.UDL5 IN ('WEIGHT', 'THICKNESS BOTTOM', 'OFF CENTER TOLERANCE')) AND (OC_VDATA.UDL6 = @MachineLineID)
AND (OC_VDAT_AUX.UDL40 BETWEEN CONVERT(DATETIME, @StartDate, 102) AND CONVERT(DATETIME, @EndDate, 102))
AND (dbo.fnItemCodeFromPartNo(LEFT(OC_VDAT_AUX.PARTNOAUX, 12)) = @ProductCode)
) AS BaseData1
PIVOT (Min(Data1) FOR TestName IN([WEIGHT],[THICKNESS BOTTOM],[OFF CENTER TOLERANCE])) as pvt
),
SecondData AS
(
SELECT OC_VDATA.UDL1 AS PLANT_CODE, dbo.fnItemCodeFromPartNo(LEFT(OC_VDAT_AUX.PARTNOAUX, 12)) AS ITEM_CODE,
OC_VDATA.UDL5 AS TestName, OC_VDAT_AUX.UDL40 AS SampleTime, OC_VDAT_AUX.UDL8 AS MachineID, OC_VDAT_AUX.UDL13 AS RowNum,
OC_VDAT_AUX.UDL21 AS RowPos, OC_VDAT_AUX.UDL12 AS Cavity, OC_VDAT_AUX.UDL18 AS TestedBy,
OC_VDATA.DATA1 AS 'THICKNESS SIDEWALL MIN', OC_VDATA.DATA2 AS 'THICKNESS SIDEWALL MAX'
FROM OC_VDAT_AUX INNER JOIN
OC_VDATA ON OC_VDAT_AUX.PARTNOAUX = OC_VDATA.PARTNO AND OC_VDAT_AUX.DATETIMEAUX = OC_VDATA.DATETIME
WHERE (OC_VDATA.UDL1 = @PlantCode) AND (OC_VDATA.UDL5 = 'THICKNESS SIDEWALL') AND
(OC_VDATA.UDL6 = @MachineLineID) AND (OC_VDAT_AUX.UDL40 BETWEEN CONVERT(DATETIME, @StartDate, 102) AND CONVERT(DATETIME, @EndDate, 102))
AND (dbo.fnItemCodeFromPartNo(LEFT(OC_VDAT_AUX.PARTNOAUX, 12)) = @ProductCode)
),
LineWeight AS
(
SELECT OC_VDATA.UDL1 AS PLANT_CODE, dbo.fnItemCodeFromPartNo(LEFT(OC_VDAT_AUX.PARTNOAUX, 12)) AS ITEM_CODE, Cast(Avg(OC_VDATA.DATA1) AS decimal(10,2)) as AverageLineWeight
FROM OC_VDAT_AUX INNER JOIN
OC_VDATA ON OC_VDAT_AUX.PARTNOAUX = OC_VDATA.PARTNO AND OC_VDAT_AUX.DATETIMEAUX = OC_VDATA.DATETIME
WHERE (OC_VDATA.UDL1 = @PlantCode) AND (OC_VDATA.UDL5 = 'WEIGHT') AND (OC_VDATA.UDL6 = @MachineLineID) AND (OC_VDAT_AUX.UDL40 BETWEEN
CONVERT(DATETIME, @StartDate, 102) AND CONVERT(DATETIME, @EndDate, 102)) AND (dbo.fnItemCodeFromPartNo(LEFT(OC_VDAT_AUX.PARTNOAUX, 12))
= @ProductCode)
GROUP BY OC_VDATA.UDL1, dbo.fnItemCodeFromPartNo(LEFT(OC_VDAT_AUX.PARTNOAUX, 12))
)
INSERT INTO #AnalysisData
SELECT f.PLANT_CODE, f.ITEM_CODE, f.SampleTime, f.MachineID, f.RowNum, f.RowPos, f.Cavity, f.TestedBy, f.WEIGHT, lw.AverageLineWeight,
s.[THICKNESS SIDEWALL MIN], s.[THICKNESS SIDEWALL MAX], f.[OFF CENTER TOLERANCE], f.[THICKNESS BOTTOM]
FROM FirstData AS f INNER JOIN
SecondData AS s ON f.PLANT_CODE = s.PLANT_CODE INNER JOIN LineWeight AS lw ON f.PLANT_CODE = lw.PLANT_CODE
AND f.ITEM_CODE = s.ITEM_CODE
AND f.SampleTime = s.SampleTime
AND f.MachineID = s.MachineID
AND f.RowNum = s.RowNum
AND f.RowPos = s.RowPos
AND f.Cavity = s.Cavity
AND f.TestedBy = s.TestedBy
-----------------------------------------------------------------------------------------
-- Create a CTE to hold the spec data
-----------------------------------------------------------------------------------------
CREATE TABLE #SpecDataFinal
(
PLANT_CODE CHAR(30),
ITEM_CODE varchar(30),
[Weight - LowerSpec] float,
[Weight - Target] float,
[Weight - UpperSpec] float,
[Thickness Sidewall - LowerSpec] float,
[Thickness Sidewall - Target] float,
[Thickness Sidewall - UpperSpec] float,
[Thickness Bottom - LowerSpec] float,
[Thickness Bottom - Target] float,
[Thickness Bottom - UpperSpec] float,
[Off Center Tolerance - LowerSpec] float,
[Off Center Tolerance - Target] float,
[Off Center Tolerance - UpperSpec] float
);
WITH SpecDataMod AS
(
SELECT PLANT_CODE, ITEM_CODE, PROPERTY + ' - ' + SpecType AS PropertyType, SpecValue FROM
(
SELECT PLANT_CODE, PARTNO, ITEM_CODE, PROPERTY, v.*
FROM vwSpecsByPlantCode
CROSS APPLY (values ('LowerSpec', INDLS), ('Target', TARGETX), ('UpperSpec', INDUS)) v(SpecType, SpecValue)
) AS SpecData
)
INSERT INTO #SpecDataFinal
SELECT * FROM(SELECT PLANT_CODE, ITEM_CODE, PropertyType, SpecValue FROM SpecDataMod) AS SpecBaseData
PIVOT (Min(SpecValue) FOR PropertyType IN([Weight - LowerSpec],[Weight - Target],[Weight - UpperSpec],[Thickness Sidewall - LowerSpec]
,[Thickness Sidewall - Target],[Thickness Sidewall - UpperSpec],[Thickness Bottom - LowerSpec],[Thickness Bottom - Target]
,[Thickness Bottom - UpperSpec],[Off Center Tolerance - LowerSpec],[Off Center Tolerance - Target],[Off Center Tolerance - UpperSpec])) as pvt
-----------------------------------------------------------------------------------------
-- Finally, merge the data together
-----------------------------------------------------------------------------------------
SELECT RTRIM(ad.PLANT_CODE) AS PLANT_CODE, RTRIM(ad.ITEM_CODE) AS ITEM_CODE, RTRIM(ad.SampleTime) AS SampleTime, RTRIM(ad.MachineID) as MachineID,
RTRIM(ad.RowNum) as RowNum, RTRIM(ad.RowPos) as RowPos, RTRIM(ad.Cavity) as Cavity, RTRIM(ad.TestedBy) AS TestedBy, RTRIM(ad.WEIGHT) as WEIGHT,
RTRIM(ad.AverageLineWeight) AS AverageLineWeight, RTRIM(ad.[THICKNESS SIDEWALL MIN]) AS [THICKNESS SIDEWALL MIN],
rtrim(ad.[THICKNESS SIDEWALL MAX]) as [THICKNESS SIDEWALL MAX], rtrim(ad.[OFF CENTER TOLERANCE]) as [OFF CENTER TOLERANCE],
rtrim(ad.[THICKNESS BOTTOM]) as [THICKNESS BOTTOM], rtrim(sd.[Weight - LowerSpec]) as [Weight - LowerSpec], rtrim(sd.[Weight - Target]) as [Weight - Target],
rtrim(sd.[Weight - UpperSpec]) as [Weight - UpperSpec], rtrim(sd.[Thickness Sidewall - LowerSpec]) as [Thickness Sidewall - LowerSpec],
rtrim(sd.[Thickness Sidewall - Target]) AS [Thickness Sidewall - Target] , rtrim(sd.[Thickness Sidewall - UpperSpec]) as [Thickness Sidewall - UpperSpec],
rtrim(sd.[Thickness Bottom - LowerSpec]) as [Thickness Bottom - LowerSpec], rtrim(sd.[Thickness Bottom - Target]) as [Thickness Bottom - Target],
rtrim(sd.[Thickness Bottom - UpperSpec]) AS [Thickness Bottom - UpperSpec], rtrim(sd.[Off Center Tolerance - LowerSpec]) as [Off Center Tolerance - LowerSpec],
rtrim(sd.[Off Center Tolerance - Target]) as [Off Center Tolerance - Target], rtrim(sd.[Off Center Tolerance - UpperSpec]) as [Off Center Tolerance - UpperSpec]
FROM #AnalysisData as ad INNER JOIN #SpecDataFinal AS sd ON ad.ITEM_CODE = sd.ITEM_CODE AND ad.PLANT_CODE = sd.PLANT_CODE
ORDER BY RowPos
--SELECT *
--FROM #AnalysisData as ad INNER JOIN #SpecDataFinal AS sd ON ad.ITEM_CODE = sd.ITEM_CODE AND ad.PLANT_CODE = sd.PLANT_CODE
--ORDER BY ad.SampleTime, ad.RowNum, ad.RowPos
DROP TABLE #SpecDataFinal
DROP TABLE #AnalysisData
END
我可以看到期望的结果。当我将数据集放入SSRS并希望使用任何聚合时,我只能在运行报告时看到FIRST聚合。如果我使用其他任何一个(如MIN,MAX,AVG等)来运行报表,则看不到报表中显示任何其他汇总。这只是报告中的空白。长话短说,我想知道我的存储过程是否与其他无法运行的聚合有关。
答案 0 :(得分:1)
运行此存储过程时,我会看到期望的输出。
这意味着存储过程运行正常。如果您没有在SSRS中看到结果,则问题出在SSRS中。
答案 1 :(得分:1)
评论太久,所以就去了。
首先,您需要进行认真的代码审查。这不是那个地方。大量的审阅者将提供有关改进代码,提高技能,提高代码效率等方面的有用评论。
简单的事情,例如使用浮点数据类型停止使用RTRIM(例如rtrim(sd。[偏心公差-目标])。为什么这样做?该列定义为浮点。您到处都修剪PlantCode。为什么?在代码的所有位置都将其作为char(30)-模式中的实际数据类型是什么?如果它的长度可变,则使用varchar(尤其是在您的参数中)。
为什么将日期时间值作为参数的字符串传递?而且您犯了另一个典型的错误-23:59:59不是datetime或datetime2的最大可能时间部分。不要使用包含上界的边界-使用包含上界的边界可以避免这种情况。顺便说一句,可能是您的float列到字符串的强制隐式转换是问题的根源。
下一步-向每个存储过程添加“ set nocount on”作为第一条语句,并触发您编写。这样可以避免向执行此逻辑的应用程序发送“受影响的x行”消息。
尝试为必须支持此代码的人添加一些有用的注释。并确保它们既准确又有用。您已经在多个地方“创建了一个保存...的cte”。首先,通常没有人关心您如何生成所需的信息。接下来,您将使用CTE填充您创建的临时表。 “创建”通常适用于要创建的对象,并且通常不将该术语与CTE一起使用。实际上,您使用了许多CTE,而不是注释中隐含的CTE。
一个好的代码审查还应该找到其他东西-包括一些最佳实践。
答案 2 :(得分:-1)
尝试强制转换或转换以进行操作。可能有效