在调用存储过程来检索值时,在某些情况下(并非所有 - 它都可以正常处理某些数据),我得到“字符串或二进制数据将被截断”错误信息。
根据this,当您尝试插入太长的数据或尝试无序添加数据时会发生这种情况;后者不是问题,因为它在某些情况下确实有效。这显然是一个数据问题。
exeption消息说“priceUsageVariance”(我的存储过程)的第75行是罪魁祸首:
“priceUsageVariance”的第75行是:
WHERE ItemCode='X'
以下是该存储过程的摘录,以显示更多上下文(表面上有问题的行是最后一行):
. . .
CREATE TABLE #TEMPCOMBINED(
PlatypusNo VARCHAR(6),
PlatypusName VARCHAR(50),
ItemCode VARCHAR(15),
PlatypusItemCode VARCHAR(20),
DuckbillDESCRIPTION VARCHAR(50),
PlatypusDESCRIPTION VARCHAR(200),
WEEK1USAGE DECIMAL(18,2),
WEEK2USAGE DECIMAL(18,2),
USAGEVARIANCE AS WEEK2USAGE - WEEK1USAGE,
WEEK1PRICE DECIMAL(18,2),
WEEK2PRICE DECIMAL(18,2),
PRICEVARIANCE AS WEEK2PRICE - WEEK1PRICE,
PRICEVARIANCEPERCENTAGE AS CAST((WEEK2PRICE - WEEK1PRICE) / NULLIF(WEEK1PRICE,0) AS DECIMAL(18,5))
);
INSERT INTO #TEMPCOMBINED (PlatypusNo, PlatypusName, ItemCode, PlatypusItemCode, DuckbillDESCRIPTION, PlatypusDESCRIPTION,
WEEK1USAGE, WEEK2USAGE, WEEK1PRICE, WEEK2PRICE)
SELECT T1.PlatypusNo, T1.PlatypusName, 'X', T1.PlatypusITEMCODE, NULL, T1.DESCRIPTION, T1.WEEK1USAGE, T2.WEEK2USAGE,
T1.WEEK1PRICE, T2.WEEK2PRICE
FROM #TEMP1 T1
LEFT JOIN #TEMP2 T2 ON T1.PlatypusITEMCODE = T2.PlatypusITEMCODE
UPDATE #TEMPCOMBINED SET ItemCode = ISNULL(
(SELECT TOP 1 ItemCode
FROM MasterPlatypusUnitMapping
WHERE Unit=@Unit
AND PlatypusNo=#TEMPCOMBINED.PlatypusNo
AND PlatypusItemCode = #TEMPCOMBINED.PlatypusItemCode
AND ItemCode IN (SELECT ItemCode FROM UnitProducts WHERE Unit=@Unit)),'X'
)
WHERE ItemCode='X'
. . .
我看不出这个问题怎么可能 - 使用MasterPlatypusUnitMapping表中的ItemCode值更新ItemCode字段 - 这是一个VarChar(15),与我的#TEMPCOMBINE表中的相应字段相同 - 或者是'X'。两个值怎么可能太大?
行号是否有效/可靠?有没有办法在处理存储过程时单步执行?
是否有某种解决方法以便我可以阻止此异常对作品进行污染?
回应Shnugo的建议/要求,这是整个SP:
这是:
CREATE Procedure [dbo].[priceAndUsageVariance]
@Unit varchar(25),
@BegDate datetime,
@EndDate datetime
AS
DECLARE @Week1End datetime = DATEADD(Day, 6, @BegDate);
DECLARE @Week2Begin datetime = DATEADD(Day, 7, @BegDate);
// temp1 holds some values for the first week
CREATE TABLE #TEMP1
(
MemberNo VARCHAR(6),
MemberName VARCHAR(50),
MEMBERITEMCODE VARCHAR(25),
DESCRIPTION VARCHAR(50),
WEEK1USAGE DECIMAL(18,2),
WEEK1PRICE DECIMAL(18,2)
);
INSERT INTO #TEMP1 (MemberNo, MemberName, MEMBERITEMCODE, DESCRIPTION,
WEEK1USAGE, WEEK1PRICE)
SELECT INVD.MEMBERNO, MemberName, ITEMCODE, DESCRIPTION, SUM(QTYSHIPPED),
PRICE
FROM INVOICEDETAIL INVD
JOIN MEMBERS M ON INVD.MEMBERNO = M.MEMBERNO
WHERE UNIT=@UNIT AND INVOICEDATE BETWEEN @BEGDATE AND @Week1End
GROUP BY ITEMCODE, DESCRIPTION, PRICE, INVD.MEMBERNO, MemberName
// temp2 holds some values for the second week
CREATE TABLE #TEMP2
(
MemberNo VARCHAR(6),
MemberName VARCHAR(50),
MEMBERITEMCODE VARCHAR(25),
DESCRIPTION VARCHAR(50),
WEEK2USAGE DECIMAL(18,2),
WEEK2PRICE DECIMAL(18,2)
);
INSERT INTO #TEMP2 (MemberNo, MemberName, MEMBERITEMCODE, DESCRIPTION,
WEEK2USAGE, WEEK2PRICE)
SELECT INVD.MEMBERNO, MemberName, ITEMCODE, DESCRIPTION, SUM(QTYSHIPPED),
PRICE
FROM INVOICEDETAIL INVD
JOIN MEMBERS M ON INVD.MEMBERNO = M.MEMBERNO
WHERE UNIT=@UNIT AND INVOICEDATE BETWEEN @Week2Begin AND @ENDDATE
GROUP BY ITEMCODE, DESCRIPTION, PRICE, INVD.MEMBERNO, MemberName
// Now tempCombined gets the shared values from temp1 as well as the unique
vals from temp1 and the unique vals from temp2
CREATE TABLE #TEMPCOMBINED(
MemberNo VARCHAR(6),
MemberName VARCHAR(50),
ItemCode VARCHAR(15),
MemberItemCode VARCHAR(20),
PlatypusDESCRIPTION VARCHAR(50),
MEMBERDESCRIPTION VARCHAR(200),
WEEK1USAGE DECIMAL(18,2),
WEEK2USAGE DECIMAL(18,2),
USAGEVARIANCE AS WEEK2USAGE - WEEK1USAGE,
WEEK1PRICE DECIMAL(18,2),
WEEK2PRICE DECIMAL(18,2),
PRICEVARIANCE AS WEEK2PRICE - WEEK1PRICE,
PRICEVARIANCEPERCENTAGE AS CAST((WEEK2PRICE - WEEK1PRICE) /
NULLIF(WEEK1PRICE,0) AS DECIMAL(18,5))
);
INSERT INTO #TEMPCOMBINED (MemberNo, MemberName, ItemCode, MemberItemCode,
PlatypusDESCRIPTION, MEMBERDESCRIPTION,
WEEK1USAGE, WEEK2USAGE, WEEK1PRICE, WEEK2PRICE)
SELECT T1.MemberNo, T1.MemberName, 'X', T1.MEMBERITEMCODE, NULL,
T1.DESCRIPTION,
T1.WEEK1USAGE, T2.WEEK2USAGE,
T1.WEEK1PRICE, T2.WEEK2PRICE
FROM #TEMP1 T1
LEFT JOIN #TEMP2 T2 ON T1.MEMBERITEMCODE = T2.MEMBERITEMCODE
// Now some mumbo-jumbo is performed to display the "general" description
rather than the "localized" description
UPDATE #TEMPCOMBINED SET ItemCode = ISNULL(
(SELECT TOP 1 ItemCode
FROM MasterMemberUnitMapping
WHERE Unit=@Unit
AND MemberNo=#TEMPCOMBINED.MemberNo
AND MemberItemCode = #TEMPCOMBINED.MemberItemCode
AND ItemCode IN (SELECT ItemCode FROM UnitProducts WHERE Unit=@Unit)),'X'
)
WHERE ItemCode='X'
UPDATE #TEMPCOMBINED SET ItemCode = ISNULL(
(SELECT TOP 1 ItemCode FROM MasterMemberMapping WHERE
MemberNo=#TEMPCOMBINED.MemberNo AND MemberItemCode + PackType =
#TEMPCOMBINED.MemberItemCode ),'X'
)
WHERE ItemCode='X'
UPDATE #TEMPCOMBINED SET PlatypusDESCRIPTION = ISNULL(MP.Description,'')
FROM #TEMPCOMBINED TC
INNER JOIN MasterProducts MP ON MP.Itemcode=TC.ItemCode
// finally, what is hoped to be the desired amalgamation is returned
SELECT TC.PlatypusDESCRIPTION, TC.MemberName, TC.WEEK1USAGE, TC.WEEK2USAGE,
TC.USAGEVARIANCE, TC.WEEK1PRICE, TC.WEEK2PRICE, TC.PRICEVARIANCE,
TC.PRICEVARIANCEPERCENTAGE
FROM #TEMPCOMBINED TC
ORDER BY TC.PlatypusDESCRIPTION, TC.MemberName;
我正在尝试对此进行现代化改造,以适应Schnugo的代码,但有了这个:
CREATE FUNCTION [dbo].[priceAndUsageVarianceTVF]
(
@Unit varchar(25),
@BegDate datetime,
@EndDate datetime
)
RETURNS TABLE
AS
RETURN
WITH Dates aS
(
SELECT DATEADD(Day, 6, @BegDate) AS Week1End
,DATEADD(Day, 7, @BegDate) AS Week2Begin
)
,Temp1 AS
(
SELECT INVD.MEMBERNO, MemberName, ITEMCODE AS MEMBERITEMCODE, DESCRIPTION, SUM(QTYSHIPPED) AS WEEK1USAGE,
PRICE AS WEEK1PRICE
FROM INVOICEDETAIL INVD
JOIN MEMBERS M ON INVD.MEMBERNO = M.MEMBERNO
WHERE UNIT=@UNIT AND INVOICEDATE BETWEEN @BEGDATE AND (SELECT Week1End FROM Dates)
GROUP BY ITEMCODE, DESCRIPTION, PRICE, INVD.MEMBERNO, MemberName
)
,Temp2 AS
(
SELECT INVD.MEMBERNO, MemberName, ITEMCODE AS MEMBERITEMCODE, DESCRIPTION, SUM(QTYSHIPPED) AS WEEK2USAGE,
PRICE AS WEEK2PRICE
FROM INVOICEDETAIL INVD
JOIN MEMBERS M ON INVD.MEMBERNO = M.MEMBERNO
WHERE UNIT=@UNIT AND INVOICEDATE BETWEEN (SELECT Week2Begin FROM Dates) AND @ENDDATE
GROUP BY ITEMCODE, DESCRIPTION, PRICE, INVD.MEMBERNO, MemberName
)
,TempCombined AS
(
SELECT T1.MemberNo, T1.MemberName, T1.MEMBERITEMCODE, NULL AS PLATYPUSDESCRIPTION,
T1.DESCRIPTION,
T1.WEEK1USAGE, T2.WEEK2USAGE,
T1.WEEK1PRICE, T2.WEEK2PRICE
FROM Temp1 T1
LEFT JOIN Temp2 T2 ON T1.MEMBERITEMCODE = T2.MEMBERITEMCODE
)
SELECT ROW_NUMBER() OVER(ORDER BY TC.PLATYPUSDESCRIPTION, TC.MemberName) AS RowInxToGetASortOrder,
ISNULL(MP.Description,'') AS PLATYPUSDESCRIPTION,
TC.MemberName, TC.WEEK1USAGE, TC.WEEK2USAGE,
TC.USAGEVARIANCE AS T2.WEEK2USAGE - T1.WEEK1USAGE,
TC.WEEK1PRICE, TC.WEEK2PRICE,
TC.PRICEVARIANCE AS T2.WEEK2PRICE - T1.WEEK1PRICE,
TC.PRICEVARIANCEPERCENTAGE AS CAST((T2.WEEK2PRICE - T1.WEEK1PRICE) / NULLIF(T1.WEEK1PRICE,0) AS DECIMAL(18,5))
FROM TempCombined TC
LEFT JOIN Temp2 T2 ON T1.MEMBERITEMCODE = T2.MEMBERITEMCODE
--LEFT JOIN MasterProducts MP ON MP.Itemcode=ISNULL(ItemCode_Try1.ItemCode, ItemCode_Try2.ItemCode)
LEFT JOIN MasterProducts MP ON MP.Itemcode=ISNULL(ItemCode_Try1.ItemCode, ItemCode_Try2.ItemCode)
CROSS APPLY
(
SELECT TOP 1 ItemCode
FROM MasterMemberUnitMapping
WHERE Unit=@Unit
AND MemberNo=TC.MemberNo
AND MemberItemCode = TC.MemberItemCode
AND ItemCode IN (SELECT ItemCode FROM UnitProducts WHERE Unit=@Unit)
) AS ItemCode_Try1(ItemCode)
CROSS APPLY
(
SELECT TOP 1 ItemCode
FROM MasterMemberMapping
WHERE MemberNo=TC.MemberNo
AND MemberItemCode + PackType = TC.MemberItemCode
) AS ItemCode_Try2(ItemCode)
;
...我收到以下错误消息:
Msg 102, Level 15, State 1, Procedure priceAndUsageVarianceTVF, Line 45
Incorrect syntax near '.'.
Msg 156, Level 15, State 1, Procedure priceAndUsageVarianceTVF, Line 61
Incorrect syntax near the keyword 'AS'.
Msg 156, Level 15, State 1, Procedure priceAndUsageVarianceTVF, Line 68
Incorrect syntax near the keyword 'AS'.
Msg 102就在这一行:
TC.USAGEVARIANCE AS T2.WEEK2USAGE - T1.WEEK1USAGE,
(在T2.WEEK2USAGE下方有红色曲线)
Msg 156位于最后两条“AS”行,即:
AS ItemCode_Try1(ItemCode)
......而且这个:
) AS ItemCode_Try2(ItemCode)
答案 0 :(得分:1)
在这种情况下我遵循的调试过程是,我将这部分存储过程代码放在单独的脚本中并在那里运行以达到根本原因。
如果您不确定哪个行或列确实导致此截断错误,则可以按照以下步骤操作。
您可以在运行程序时注释显示为问题的同一行(例如,where item ='x')并运行脚本以确定它是否是实际的错误行。
如果您在截断错误后仍然评论该行,则表示错误在其他地方。您可以在select语句中逐个注释列,并且每次都运行脚本以找出确实抛出此错误的列。
我不确定#temp1和#temp2的表定义是什么,你可以对#tempcombine进行交叉验证以确认匹配的数据类型和长度。
答案 1 :(得分:1)
我仍然不知道原因,可能是你连接MemberItemCode + PackType
...
可是:
这样的StoredProcedure非常老式这是一个经典的例子,其中一个可内联(ad-hoc)表值函数将是一个更好的方法。
在不知道您的数据库且没有机会测试某些东西的情况下,以下建议肯定不会“开箱即用”,但您可能会知道如何加快速度。作为一个副作用,您将摆脱提到的错误,因为查询引擎将处理原始列,并且不必将值从一个位置复制到具有截断效果的(最终较小的)列:
我敢肯定,这个结构远离最佳状态,但是在不知道细节的情况下,我没有其他机会复制和调整你在SP中定义的代码块。特别是在你的“mumbo-jumbo”中:-)我不太确定,如果我的想法是正确的......
CREATE FUNCTION [dbo].[priceAndUsageVariance]
(
@Unit varchar(25),
@BegDate datetime,
@EndDate datetime
)
RETURNS TABLE
AS
RETURN
WITH Dates aS
(
SELECT DATEADD(Day, 6, @BegDate) AS Week1End
,DATEADD(Day, 7, @BegDate) AS Week2Begin
)
,Temp1 AS
(
SELECT INVD.MEMBERNO, MemberName, ITEMCODE, DESCRIPTION, SUM(QTYSHIPPED),
PRICE
FROM INVOICEDETAIL INVD
JOIN MEMBERS M ON INVD.MEMBERNO = M.MEMBERNO
WHERE UNIT=@UNIT AND INVOICEDATE BETWEEN @BEGDATE AND (SELECT Week1End FROM Dates)
GROUP BY ITEMCODE, DESCRIPTION, PRICE, INVD.MEMBERNO, MemberName
)
,Temp2 AS
(
SELECT INVD.MEMBERNO, MemberName, ITEMCODE, DESCRIPTION, SUM(QTYSHIPPED),
PRICE
FROM INVOICEDETAIL INVD
JOIN MEMBERS M ON INVD.MEMBERNO = M.MEMBERNO
WHERE UNIT=@UNIT AND INVOICEDATE BETWEEN (SELECT Week2Begin FROM Dates) AND @ENDDATE
GROUP BY ITEMCODE, DESCRIPTION, PRICE, INVD.MEMBERNO, MemberName
)
,TempCombined AS
(
SELECT T1.MemberNo, T1.MemberName, 'X', T1.MEMBERITEMCODE, NULL,
T1.DESCRIPTION,
T1.WEEK1USAGE, T2.WEEK2USAGE,
T1.WEEK1PRICE, T2.WEEK2PRICE
FROM Temp1 T1
LEFT JOIN Temp2 T2 ON T1.MEMBERITEMCODE = T2.MEMBERITEMCODE
)
SELECT ROW_NUMBER() OVER(ORDER BY TC.PlatypusDESCRIPTION, TC.MemberName) AS RowInxToGetASortOrder,
ISNULL(MP.Description,'') AS PlatypusDESCRIPTION,
TC.MemberName, TC.WEEK1USAGE, TC.WEEK2USAGE,
TC.USAGEVARIANCE, TC.WEEK1PRICE, TC.WEEK2PRICE, TC.PRICEVARIANCE,
TC.PRICEVARIANCEPERCENTAGE
FROM TempCombined TC
LEFT JOIN MasterProducts MP ON MP.Itemcode=ISNULL(ItemCode_Try1.ItemCode,ItemCode_Try2.ItemCode)
CROSS APPLY
(
SELECT TOP 1 ItemCode
FROM MasterMemberUnitMapping
WHERE Unit=@Unit
AND MemberNo=TC.MemberNo
AND MemberItemCode = TC.MemberItemCode
AND ItemCode IN (SELECT ItemCode FROM UnitProducts WHERE Unit=@Unit)
) AS ItemCode_Try1(ItemCode)
CROSS APPLY
(
SELECT TOP 1 ItemCode
FROM MasterMemberMapping
WHERE MemberNo=TC.MemberNo
AND MemberItemCode + PackType = TC.MemberItemCode
) AS ItemCode_Try2(ItemCode)
;
答案 2 :(得分:1)
这是您尝试使您的方法现代化的答案。
您尝试在中央SELECT
中设置错误的别名将其更改为此,它将不再存在语法问题:
SELECT ROW_NUMBER() OVER(ORDER BY TC.PLATYPUSDESCRIPTION, TC.MemberName) AS RowInxToGetASortOrder,
ISNULL(MP.Description,'') AS PLATYPUSDESCRIPTION,
TC.MemberName, TC.WEEK1USAGE, TC.WEEK2USAGE,
T2.WEEK2USAGE - T1.WEEK1USAGE AS TC_USAGEVARIANCE,
TC.WEEK1PRICE, TC.WEEK2PRICE,
T2.WEEK2PRICE - T1.WEEK1PRICE AS TC_PRICEVARIANCE,
CAST((T2.WEEK2PRICE - T1.WEEK1PRICE) / NULLIF(T1.WEEK1PRICE,0) AS DECIMAL(18,5)) AS TC_PRICEVARIANCEPERCENTAGE
FROM TempCombined TC
但是 - 当然 - 我不知道,如果它能提供正确的结果......
答案 3 :(得分:0)
我所做的是增加其中一个Description值的大小以匹配另一个的大小,从中得出:
DuckbillDESCRIPTION VARCHAR(50),
PlatypusDESCRIPTION VARCHAR(200),
......对此:
DuckbillDESCRIPTION VARCHAR(200),
PlatypusDESCRIPTION VARCHAR(200),
...而且我还将 IsNull()添加到每个字段,这样我总是得到一个零,空字符串,或者一个" boo boo"字符串,永远不是空值。