我们在2005年到2014年间进行了SQL Server升级。升级后,其中一个存储过程运行速度非常慢。过去需要大约10分钟,现在增加到23分钟。
在进一步调查中,新升级后的执行时间非常好,只有在执行3-4次后才会增加。我尝试再次恢复新数据库并确认上述行为。有人可以帮助我,因为我不知道发生了什么!
我是核心DBA,而不是应用程序DBA,所以我不太了解存储过程的行为方式。任何帮助将不胜感激。
我正在使用参数运行:
exec [usp_RecalculateMV] '10', 8, 2015
存储过程代码:
CREATE PROCEDURE [dbo].[usp_RecalculateMV]
(@MarketID nvarchar(100),
@FromMonth int,
@FromYear int)
AS
BEGIN
--variable to hold any errors generated by the procedure
DECLARE @ErrorCode INT
DECLARE @MarketIDs INT
DECLARE @Failed nvarchar(100)
DECLARE @SplitBy nvarchar(10)
DECLARE @StartDate nvarchar(11)
--
-- Changes done by Rupan
-- Start (SER000008289)
DECLARE @cQtytoSplit FLOAT
DECLARE @cOtherQty FLOAT
DECLARE @cSplitThreshold INT
DECLARE @cWeeklySplit NVARCHAR(35)
DECLARE @cWeeklyBufferSplit NVARCHAR(35)
DECLARE @SplitType VARCHAR(15)
DECLARE @cSplitValue FLOAT
DECLARE @jSplit INT
SET @jSplit = 1
DECLARE @iSplit INT
SET @iSplit = 1
DECLARE @cPackCode NVARCHAR(50)
DECLARE @cCustCode NVARCHAR(50)
DECLARE @Week1Cursor INT
DECLARE @Week2Cursor INT
DECLARE @Week3Cursor INT
DECLARE @Week4Cursor INT
DECLARE @QtytoSplitCursor FLOAT
DECLARE @OtherQtyCursor FLOAT
DECLARE @itemp INT
SET @itemp = 1
DECLARE @tempCount INT
DECLARE @tempPackCode NVARCHAR(50)
DECLARE @tempCustomerCode NVARCHAR(50)
DECLARE @tempWeeklyMV VARCHAR(85)
DECLARE @tempWeeklBuffer VARCHAR(85)
DECLARE @tempMVQTY FLOAT
DECLARE @tempBufferQTY FLOAT
CREATE TABLE #tmp_IntialLoad ( ID INT IDENTITY(1,1),
MVQty FLOAT,
BufferQty FLOAT,
SplitThreshold INT,
WeeklyMVQty NVARCHAR(50) COLLATE SQL_Latin1_General_CP1_CI_AS,
WeeklyBufferQty NVARCHAR(50) COLLATE SQL_Latin1_General_CP1_CI_AS,
PackCode NVARCHAR(50) COLLATE SQL_Latin1_General_CP1_CI_AS,
CustomerCode NVARCHAR(85) COLLATE SQL_Latin1_General_CP1_CI_AS
);
CREATE TABLE #tmp_SplitQTY ( ID INT IDENTITY(1,1),
SplitType VARCHAR(15) COLLATE SQL_Latin1_General_CP1_CI_AS,
QtytoSplit FLOAT,
OtherQty FLOAT,
MarketID NVARCHAR(85) COLLATE SQL_Latin1_General_CP1_CI_AS,
Code NVARCHAR(85) COLLATE SQL_Latin1_General_CP1_CI_AS,
CustomerCode NVARCHAR(85) COLLATE SQL_Latin1_General_CP1_CI_AS,
Week1 INT,
Week2 INT,
Week3 INT,
Week4 INT,
Week1Buffer INT,
Week2Buffer INT,
Week3Buffer INT,
Week4Buffer INT
);
CREATE TABLE #tmp_SplitBufferQTY ( ID INT IDENTITY(1,1),
SplitType VARCHAR(15) COLLATE SQL_Latin1_General_CP1_CI_AS,
QtytoSplit FLOAT,
OtherQty FLOAT,
MarketID NVARCHAR(85) COLLATE SQL_Latin1_General_CP1_CI_AS,
Code NVARCHAR(85) COLLATE SQL_Latin1_General_CP1_CI_AS,
CustomerCode NVARCHAR(85) COLLATE SQL_Latin1_General_CP1_CI_AS,
Week1 INT,
Week2 INT,
Week3 INT,
Week4 INT
);
CREATE TABLE #tmp_FinalSplitQTY ( ID INT IDENTITY(1,1),
MarketID NVARCHAR(85) COLLATE SQL_Latin1_General_CP1_CI_AS,
Code NVARCHAR(85) COLLATE SQL_Latin1_General_CP1_CI_AS,
CustomerCode NVARCHAR(85) COLLATE SQL_Latin1_General_CP1_CI_AS,
Week1 INT,
Week2 INT,
Week3 INT,
Week4 INT,
Week1Buffer INT,
Week2Buffer INT,
Week3Buffer INT,
Week4Buffer INT,
QtytoSplit FLOAT,
OtherQty FLOAT
);
-- END
--table struc to hold the split string passed in above
DECLARE @T_TBL_Market TABLE
(
ID INT IDENTITY(1,1),
MarketID int --nvarchar(100)
)
--populate the temporary table with the split market id's
INSERT INTO @T_TBL_Market
SELECT * FROM dbo.Split(@MarketID, ',')
SET @StartDate = dbo.GetSafeDateFormat(1, @FromMonth, @FromYear)
--create a cursor to loop through the market id's
/* Start - Rupan
DECLARE MarketCursor CURSOR FOR
SELECT MarketID FROM @T_TBL_Market
--Open
OPEN MarketCursor
--loop through the markets
FETCH NEXT FROM MarketCursor
INTO @MarketIDs
******* End */
DECLARE @iMarketID INT
SET @iMarketID = 1
DECLARE @iCount INT
SELECT @iCount = COUNT(*) FROM @T_TBL_Market
/* Start - Rupan
WHILE @@FETCH_STATUS = 0
End */
WHILE( @iMarketID <= @iCount )
BEGIN
SELECT @MarketIDs = MarketID FROM @T_TBL_Market WHERE ID = @iMarketID
Print 'Inside While'
--Changes done by Parag on 08-04-2010, inserting values for customer detail
--Deleting old values
DELETE from TBL_CUSTOMER_CODE
INSERT INTO TBL_CUSTOMER_CODE(CustomerSplitID,CustomerID, Week1Split, Week2Split, Week3Split, Week4Split, SplitThreshold, CustomerCode)
SELECT dbo.TBL_CUSTOMER_SPLIT.CustomerSplitID, dbo.TBL_CUSTOMER.CustomerID, ISNULL(dbo.TBL_CUSTOMER_SPLIT.Week1Split, 25) AS Week1Split,
ISNULL(dbo.TBL_CUSTOMER_SPLIT.Week2Split, 25) AS Week2Split, ISNULL(dbo.TBL_CUSTOMER_SPLIT.Week3Split, 25) AS Week3Split,
ISNULL(dbo.TBL_CUSTOMER_SPLIT.Week4Split, 25) AS Week4Split, dbo.TBL_CUSTOMER_SPLIT.SplitThreshold,
ISNULL(dbo.TBL_CUSTOMER.CustomerCdSoldTo, '') + ISNULL(dbo.TBL_CUSTOMER.CustomerCdShipTo, '') AS CustomerCode
FROM dbo.TBL_CUSTOMER LEFT OUTER JOIN
dbo.TBL_CUSTOMER_SPLIT ON dbo.TBL_CUSTOMER.CustomerID = dbo.TBL_CUSTOMER_SPLIT.CustomerID
WHERE TBL_CUSTOMER.marketid =@MarketIDs
--Changes end
--start a transaction to roll back if things go awry
BEGIN TRAN
--delete the data for this market including and after the startdate
Print'Before Delete'
DELETE FROM TBL_CALCULATED_MV
WHERE MarketID = @MarketIDs
AND [Month] >= @StartDate
Print'After Delete'
--check for errors
SELECT @ErrorCode = @@Error
--if no errors continue
IF @ErrorCode = 0
BEGIN
Print'Inserting into CalculatedMV table'
--do the basic recalculation of the MV and Buffer
INSERT INTO TBL_CALCULATED_MV(
MarketName, GroupDescription, MCCode,
ChannelDesc, PackCode, PackDesc, [Month],
MVQty,BufferQty,BufferPercentage,
LocalCustomerCode,LocalCustomerName,
CustomerCdSoldTo, CustomerCdShipTo,
BrandName, BrandCode,
MinMV, MarketID,
IntlCode, EPVOrgCode, AVOrgCode, LDDQty
)
SELECT DISTINCT TBL_MARKET_COMPANY.MarketName, TBL_GROUP.GroupDescription, TBL_MARKET_COMPANY.MCCode,
TBL_CHANNEL.ChannelDesc, VW_BASE_MV.PackCode, TBL_MARKET_PACK.PackDesc, VW_BASE_MV.[Month],
VW_BASE_MV.RoundedBaseMV AS MVQty,
dbo.CalculateBuffer(VW_BASE_MV.RoundedBaseMV, VW_LATEST_BUFFER.MonthValue, VW_LATEST_MINMV.MonthValue) AS BufferQty,
VW_LATEST_BUFFER.MonthValue AS BufferPercentage,
CASE WHEN TBL_CUSTOMER.CustomerCdShipTo IS NULL THEN
TBL_CUSTOMER.CustomerCdSoldTo
ELSE
TBL_CUSTOMER.CustomerCdShipTo
END AS LocalCustomerCode,
TBL_CUSTOMER.CustDescription, TBL_CUSTOMER.CustomerCdSoldTo, TBL_CUSTOMER.CustomerCdShipTo,
TBL_BRAND.BrandName, TBL_BRAND.BrandCode,
VW_LATEST_MINMV.MonthValue, VW_BASE_MV.MarketID,
TBL_MARKET_PACK.IntlCode, TBL_MARKET_COMPANY.EPVOrgCode, TBL_MARKET_COMPANY.AVOrgCode,
VW_BASE_MV.LDD
FROM TBL_CHANNEL
INNER JOIN VW_BASE_MV
ON TBL_CHANNEL.ChannelID = VW_BASE_MV.ChannelID
INNER JOIN TBL_MARKET_COMPANY
ON TBL_MARKET_COMPANY.MarketID = VW_BASE_MV.MarketID
INNER JOIN TBL_GROUP
ON TBL_GROUP.GroupID = TBL_MARKET_COMPANY.GroupID
INNER JOIN TBL_MARKET_PACK
ON VW_BASE_MV.MarketID = TBL_MARKET_PACK.MarketID
AND VW_BASE_MV.PackCode = TBL_MARKET_PACK.PackCode
INNER JOIN TBL_BRAND
ON TBL_BRAND.BrandID = TBL_MARKET_PACK.BrandID
LEFT OUTER JOIN TBL_CUSTOMER
ON VW_BASE_MV.CustomerID = TBL_CUSTOMER.CustomerID
LEFT OUTER JOIN VW_LATEST_BUFFER
ON VW_BASE_MV.MarketID = VW_LATEST_BUFFER.MarketID
AND VW_BASE_MV.PackCode = VW_LATEST_BUFFER.PackCode
AND VW_BASE_MV.ChannelID = VW_LATEST_BUFFER.ChannelID
AND VW_BASE_MV.[Month] = VW_LATEST_BUFFER.[Month]
LEFT OUTER JOIN VW_LATEST_MINMV
ON VW_BASE_MV.MarketID = VW_LATEST_MINMV.MarketID
AND VW_BASE_MV.PackCode = VW_LATEST_MINMV.PackCode
AND VW_BASE_MV.ChannelID = VW_LATEST_MINMV.ChannelID
AND VW_BASE_MV.[Month] = VW_LATEST_MINMV.[Month]
WHERE VW_BASE_MV.[Month] >= @StartDate
AND VW_BASE_MV.MarketID = @MarketIDs
AND VW_BASE_MV.RoundedBaseMV > 0
AND CASE WHEN TBL_CUSTOMER.CustomerCdShipTo IS NULL THEN
TBL_CUSTOMER.CustomerCdSoldTo
ELSE
TBL_CUSTOMER.CustomerCdShipTo
END IS NOT NULL --prevents customer still in the market share table
--but not in the customer table from being passed through
--to the calculated mv table
--check for errors
SELECT @ErrorCode = @@Error
Print 'After Inserting into CalculatedMV table'
END
IF @ErrorCode = 0 -- Start End of Errorcode 1
BEGIN
--find out if this market splits by Pack or by customer
SELECT @SplitBy = ISNULL(SplitBy, 'Unassigned')
FROM TBL_MARKET_COMPANY
WHERE MarketID = @MarketIDs
--calculate the split MV and default the SumWeeklyMV and SumWeeklyBuffer
--fields to the MV and Buffer quantities
IF ISNULL(@SplitBy, 'Unassigned') = 'Unassigned'
BEGIN
UPDATE TBL_CALCULATED_MV
SET
Week1MV = 0,
Week2MV = 0,
Week3MV = 0,
Week4MV = 0,
Week1Buffer = 0,
Week2Buffer = 0,
Week3Buffer = 0,
Week4Buffer = 0,
SumWeeklyMV = MVQty,
SumWeeklyBuffer = BufferQty,
Split = 0
WHERE
TBL_CALCULATED_MV.MarketID = @MarketIDs
AND
TBL_CALCULATED_MV.[Month] >= @StartDate
END
IF @SplitBy = 'Pack' -- Start of Pack
BEGIN
Print 'Entering Pack sum update'
UPDATE TBL_CALCULATED_MV
SET
SumWeeklyMV = MVQty,
SumWeeklyBuffer = BufferQty,
Split = CASE WHEN MP.SplitThreshold IS NULL THEN
0
WHEN MVQty + BufferQty < MP.SplitThreshold THEN
0
ELSE
1
END
FROM TBL_CALCULATED_MV
LEFT OUTER JOIN
VW_PACK_SPLIT
ON
TBL_CALCULATED_MV.PackCode = VW_PACK_SPLIT.PackCode
AND
TBL_CALCULATED_MV.MarketID = VW_PACK_SPLIT.MarketID
INNER JOIN
TBL_MARKET_PACK MP
ON
TBL_CALCULATED_MV.MarketID = MP.MarketID
AND
TBL_CALCULATED_MV.PackCode = MP.PackCode
WHERE
TBL_CALCULATED_MV.MarketID = @MarketIDs
AND
TBL_CALCULATED_MV.[Month] >= @StartDate
Print 'After sum update'
Print'Before inserting into Initialload table'
/* Changes Done By Rupan (SER00008289)*/
INSERT INTO #tmp_IntialLoad
SELECT DISTINCT
MVQty,
BufferQty,
MP.SplitThreshold,
CONVERT(VARCHAR,Week1Split)+','+CONVERT(VARCHAR,Week2Split)+','+CONVERT(VARCHAR,Week3Split)+','+CONVERT(VARCHAR,Week4Split),
CONVERT(VARCHAR,Week1Split)+','+CONVERT(VARCHAR,Week2Split)+','+CONVERT(VARCHAR,Week3Split)+','+CONVERT(VARCHAR,Week4Split),
MP.PackCode,
TBL_CALCULATED_MV.LocalCustomerCode
FROM
TBL_CALCULATED_MV
LEFT OUTER JOIN VW_PACK_SPLIT
ON TBL_CALCULATED_MV.PackCode = VW_PACK_SPLIT.PackCode
AND TBL_CALCULATED_MV.MarketID = VW_PACK_SPLIT.MarketID
INNER JOIN TBL_MARKET_PACK MP
ON TBL_CALCULATED_MV.MarketID = MP.MarketID
AND TBL_CALCULATED_MV.PackCode = MP.PackCode
WHERE TBL_CALCULATED_MV.MarketID = @MarketIDs
AND TBL_CALCULATED_MV.[Month] >= @StartDate
AND TBL_CALCULATED_MV.Split=1
SET @jSplit = 1
Print 'After Initial load and before looping Initial Load'
WHILE(@jSplit <= (SELECT Count(*) FROM #tmp_IntialLoad ))
BEGIN
SELECT @cQtytoSplit = MVQty,
@cOtherQty = BufferQty,
@cSplitThreshold = SplitThreshold,
@cWeeklySplit = WeeklyMVQty,
@cWeeklyBufferSplit = WeeklyBufferQty,
@cPackCode = PackCode,
@cCustCode = CustomerCode
FROM
#tmp_IntialLoad
WHERE
ID = @jSplit
INSERT INTO #tmp_SplitQTY(SplitType,QtytoSplit,OtherQty,MarketID,Code,CustomerCode,Week1,Week2,Week3,Week4)
EXEC CalculateSplitMV_working_SER8289 @cQtytoSplit,@cOtherQty,@cSplitThreshold,@cWeeklySplit,'MV',@MarketIDs,@cPackCode,NULL
INSERT INTO #tmp_SplitBufferQTY(SplitType,QtytoSplit,OtherQty,MarketID,Code,CustomerCode,Week1,Week2,Week3,Week4)
EXEC CalculateSplitMV_working_SER8289 @cOtherQty,@cQtytoSplit,@cSplitThreshold,@cWeeklyBufferSplit,'BUFFER',@MarketIDs,@cPackCode,NULL
SET @jSplit = @jSplit + 1
END
Print 'After looping Initial Load and before updating #tmp_SplitQTY'
UPDATE ts
SET
ts.Week1Buffer = tsb.Week1,
ts.Week2Buffer = tsb.Week2,
ts.Week3Buffer = tsb.Week3,
ts.Week4Buffer = tsb.Week4
FROM
#tmp_SplitQTY ts,#tmp_SplitBufferQTY tsb
WHERE
ts.ID = tsb.ID
AND
ts.MarketID = tsb.MarketID
AND
ts.Code = tsb.Code
SELECT @tempCount = COUNT(*) FROM #tmp_SplitQTY
Print 'After updating #tmp_SplitQTY and before calling usp_Forecast_Calc_BoxSize sp '
WHILE(@itemp <= @tempCount)
BEGIN
SELECT @tempPackCode = Code,
@tempCustomerCode = CustomerCode,
@tempWeeklyMV = CONVERT(VARCHAR,Week1)+','+CONVERT(VARCHAR,Week2)+','+CONVERT(VARCHAR,Week3)+','+CONVERT(VARCHAR,Week4),
@tempWeeklBuffer = CONVERT(VARCHAR,Week1Buffer)+','+CONVERT(VARCHAR,Week2Buffer)+','+CONVERT(VARCHAR,Week3Buffer)+','+CONVERT(VARCHAR,Week4Buffer),
@tempMVQTY = QtytoSplit,
@tempBufferQTY = OtherQty
FROM
#tmp_SplitQTY
WHERE
ID = @itemp
INSERT INTO #tmp_FinalSplitQTY(MarketID,Code,CustomerCode,Week1,Week2,Week3,Week4,Week1Buffer,Week2Buffer,Week3Buffer,Week4Buffer,QtytoSplit,OtherQty)
EXEC usp_Forecast_Calc_BoxSize @tempWeeklyMV,@tempWeeklBuffer,@MarketIDs,@tempPackCode,NULL,@tempMVQTY,@tempBufferQTY
-- EXEC usp_Forecast_Calc_BoxSize '1,0,0,0','1,1,1,0',5,1120200,NULL,1,3
SET @itemp = @itemp + 1
END
PRINT 'after usp_Forecast_Calc_BoxSize and before Entering MV/Buffer Update'
/* Updating MV Quantity Weekly Split value */
UPDATE tc
SET
tc.Week1MV = tfs.Week1,
tc.Week2MV = tfs.Week2,
tc.Week3MV = tfs.Week3,
tc.Week4MV = tfs.Week4,
tc.Week1Buffer = tfs.Week1Buffer,
tc.Week2Buffer = tfs.Week2Buffer,
tc.Week3Buffer = tfs.Week3Buffer,
tc.Week4Buffer = tfs.Week4Buffer
FROM
TBL_CALCULATED_MV tc,#tmp_FinalSplitQTY tfs
WHERE
tc.PackCode = tfs.code
AND
tc.MarketID = tfs.MarketID
AND
tc.MarketID = @MarketIDs
AND
tc.MVQty = tfs.QtytoSplit
AND
tc.BufferQty = tfs.OtherQty
AND
tc.[Month]>= @StartDate
AND
tc.Split=1
PRINT 'after MV/Buffer Update'
END -- End of Pack Condition
IF @SplitBy = 'Customer' -- Start of Customer condition
BEGIN
Print 'Entering Customer Sum update'
UPDATE TBL_CALCULATED_MV
SET
SumWeeklyMV = MVQty,
SumWeeklyBuffer = BufferQty,
Split = CASE WHEN MP.SplitThreshold IS NULL THEN
0
WHEN MVQty + BufferQty < MP.SplitThreshold THEN
0
ELSE
1
END
FROM TBL_CALCULATED_MV
--LEFT OUTER JOIN VW_CUSTOMER_SPLIT
LEFT OUTER JOIN TBL_CUSTOMER_CODE
ON TBL_CALCULATED_MV.LocalCustomerCode = TBL_CUSTOMER_CODE.CustomerCode
--ON TBL_CALCULATED_MV.LocalCustomerCode = VW_CUSTOMER_SPLIT.CustomerCode
INNER JOIN TBL_MARKET_PACK MP
ON TBL_CALCULATED_MV.MarketID = MP.MarketID
AND TBL_CALCULATED_MV.PackCode = MP.PackCode
WHERE TBL_CALCULATED_MV.MarketID = @MarketIDs
AND TBL_CALCULATED_MV.[Month] >= @StartDate
--* Updating MV Quantity Weekly Split value */
/* Updating MV Quantity Weekly Split value */
Print 'After Customer Sum update'
Print'Before inserting into Initialload table'
INSERT INTO #tmp_IntialLoad
SELECT DISTINCT
MVQty,
BufferQty,
MP.SplitThreshold,
CONVERT(VARCHAR,Week1Split)+','+CONVERT(VARCHAR,Week2Split)+','+CONVERT(VARCHAR,Week3Split)+','+CONVERT(VARCHAR,Week4Split),
CONVERT(VARCHAR,Week1Split)+','+CONVERT(VARCHAR,Week2Split)+','+CONVERT(VARCHAR,Week3Split)+','+CONVERT(VARCHAR,Week4Split),
TBL_CALCULATED_MV.PackCode,
TBL_CALCULATED_MV.LocalCustomerCode
FROM
TBL_CALCULATED_MV
--LEFT OUTER JOIN VW_CUSTOMER_SPLIT
LEFT OUTER JOIN TBL_CUSTOMER_CODE
ON TBL_CALCULATED_MV.LocalCustomerCode = TBL_CUSTOMER_CODE.CustomerCode
--ON TBL_CALCULATED_MV.LocalCustomerCode = VW_CUSTOMER_SPLIT.CustomerCode
INNER JOIN TBL_MARKET_PACK MP
ON TBL_CALCULATED_MV.MarketID = MP.MarketID
AND TBL_CALCULATED_MV.PackCode = MP.PackCode
WHERE TBL_CALCULATED_MV.MarketID = @MarketIDs
AND TBL_CALCULATED_MV.[Month] >= @StartDate
AND TBL_CALCULATED_MV.Split=1
SET @jSplit = 1
Print 'After Initial load and before looping Initial Load'
WHILE(@jSplit <= (SELECT Count(*) FROM #tmp_IntialLoad ))
BEGIN
SELECT @cQtytoSplit = MVQty,
@cOtherQty = BufferQty,
@cSplitThreshold = SplitThreshold,
@cWeeklySplit = WeeklyMVQty,
@cWeeklyBufferSplit = WeeklyBufferQty,
@cPackCode = PackCode,
@cCustCode = CustomerCode
FROM
#tmp_IntialLoad
WHERE
ID = @jSplit
INSERT INTO #tmp_SplitQTY(SplitType,QtytoSplit,OtherQty,MarketID,Code,CustomerCode,Week1,Week2,Week3,Week4)
EXEC CalculateSplitMV_working_SER8289 @cQtytoSplit,@cOtherQty,@cSplitThreshold,@cWeeklySplit,'MV',@MarketIDs,@cPackCode,@cCustCode
INSERT INTO #tmp_SplitBufferQTY(SplitType,QtytoSplit,OtherQty,MarketID,Code,CustomerCode,Week1,Week2,Week3,Week4)
EXEC CalculateSplitMV_working_SER8289 @cOtherQty,@cQtytoSplit,@cSplitThreshold,@cWeeklyBufferSplit,'BUFFER',@MarketIDs,@cPackCode,@cCustCode
SET @jSplit = @jSplit + 1
END
Print 'After looping Initial Load and before updating #tmp_SplitQTY'
UPDATE ts
SET
ts.Week1Buffer = tsb.Week1,
ts.Week2Buffer = tsb.Week2,
ts.Week3Buffer = tsb.Week3,
ts.Week4Buffer = tsb.Week4
FROM
#tmp_SplitQTY ts,#tmp_SplitBufferQTY tsb
WHERE
ts.ID = tsb.ID
AND
ts.MarketID = tsb.MarketID
AND
ts.Code = tsb.Code
AND
ts.CustomerCode = tsb.CustomerCode
SELECT @tempCount = COUNT(*) FROM #tmp_SplitQTY
Print 'After updating #tmp_SplitQTY and before calling usp_Forecast_Calc_BoxSize sp '
WHILE(@itemp <= @tempCount)
BEGIN
SELECT @tempPackCode = Code,
@tempCustomerCode = CustomerCode,
@tempWeeklyMV = CONVERT(VARCHAR,Week1)+','+CONVERT(VARCHAR,Week2)+','+CONVERT(VARCHAR,Week3)+','+CONVERT(VARCHAR,Week4),
@tempWeeklBuffer = CONVERT(VARCHAR,Week1Buffer)+','+CONVERT(VARCHAR,Week2Buffer)+','+CONVERT(VARCHAR,Week3Buffer)+','+CONVERT(VARCHAR,Week4Buffer),
@tempMVQTY = QtytoSplit,
@tempBufferQTY = OtherQty
FROM
#tmp_SplitQTY
WHERE
ID = @itemp
INSERT INTO #tmp_FinalSplitQTY(MarketID,Code,CustomerCode,Week1,Week2,Week3,Week4,Week1Buffer,Week2Buffer,Week3Buffer,Week4Buffer,QtytoSplit,OtherQty)
EXEC usp_Forecast_Calc_BoxSize @tempWeeklyMV,@tempWeeklBuffer,@MarketIDs,@tempPackCode,@tempCustomerCode,@tempMVQTY,@tempBufferQTY
SET @itemp = @itemp + 1
END
PRINT 'after usp_Forecast_Calc_BoxSize and before Entering MV/Buffer Update'
/* Updating MV Quantity Weekly Split value */
UPDATE tc
SET
tc.Week1MV = tfs.Week1,
tc.Week2MV = tfs.Week2,
tc.Week3MV = tfs.Week3,
tc.Week4MV = tfs.Week4,
tc.Week1Buffer = tfs.Week1Buffer,
tc.Week2Buffer = tfs.Week2Buffer,
tc.Week3Buffer = tfs.Week3Buffer,
tc.Week4Buffer = tfs.Week4Buffer
FROM
TBL_CALCULATED_MV tc,#tmp_FinalSplitQTY tfs
WHERE
tc.PackCode = tfs.code
AND
tc.MarketID = tfs.MarketID
AND
tc.LocalCustomerCode = tfs.CustomerCode
AND
tc.MarketID = @MarketIDs
AND
tc.MVQty = tfs.QtytoSplit
AND
tc.BufferQty = tfs.OtherQty
AND
tc.[Month]>= @StartDate
AND
tc.Split=1
/* End */
PRINT 'after MV/Buffer Update'
--check for errors
SELECT @ErrorCode = @@Error
END -- End of Customer condition
END -- End of Errorcode 1
IF @ErrorCode = 0
BEGIN
Print 'Inside sumweekly update'
--Update the SumWeeklyMV and SumWeeklyBuffer fields to be the sum of the
--splits for the rows that are to be split
UPDATE TBL_CALCULATED_MV
SET
TBL_CALCULATED_MV.SumWeeklyMV = ISNULL(Week1MV,0) + ISNULL(Week2MV,0) + ISNULL(Week3MV,0) + ISNULL(Week4MV,0),
TBL_CALCULATED_MV.SumWeeklyBuffer = ISNULL(Week1Buffer,0) + ISNULL(Week2Buffer,0) + ISNULL(Week3Buffer,0) + ISNULL(Week4Buffer,0)
WHERE TBL_CALCULATED_MV.Split=1
and TBL_CALCULATED_MV.MarketID = @MarketIDs
AND TBL_CALCULATED_MV.[Month] >= @StartDate
--check for errors
select @ErrorCode = @@Error
END
IF @ErrorCode = 0
COMMIT TRAN
ELSE
BEGIN
ROLLBACK TRAN
set @Failed = @Failed + ',' + @MarketIDs
END
SET @ErrorCode = 0
/* Start - Rupan
FETCH NEXT FROM MarketCursor
INTO @MarketIDs
End */
DELETE FROM #tmp_IntialLoad
DELETE FROM #tmp_SplitQTY
DELETE FROM #tmp_FinalSplitQTY
SET @iMarketID = @iMarketID + 1
END
/*CLOSE MarketCursor
DEALLOCATE MarketCursor
*/
DROP TABLE #tmp_IntialLoad
DROP TABLE #tmp_SplitQTY
DROP TABLE #tmp_FinalSplitQTY
--IF @Failed is null
--SELECT @SuccessFail = 'All markets were successfully re-calculated'
--ELSE
-- SELECT @SuccessFail=@Failed
END -- END of Procedure Begin
GO
答案 0 :(得分:0)
如果还没有完成,你应该: