帮我看看哈!谢谢!
我想改下这个存储过程,使得在删除操作后将表BW_CannibalizeList的InCargoID 更新为前面插入操作
里刚插入记录的CargoID(自动增长型)
CREATE PROCEDURE dbo.BW_CO_Cargo_UpdateCargeForAudtor1
(
@CannibalizeID INT
)
AS
SET NOCOUNT ON
SET XACT_ABORT ON
DECLARE @InCargoID INT,
@PieceQuantity INT,
@LegalQuantity INT,
@GrossWeight DECIMAL(10,2),
@NetWeight DECIMAL(10,2),
@Volume DECIMAL(10,2),
@InDepotID INT,
@OutDepotID INT
DECLARE @CargoNO varchar(10),
@DepotID INT,
@CustomerOrderID INT,
@Code_T varchar(20),
@CargoName nvarchar(20),
@TypeOrMarks varchar(10),
@Sign nvarchar(10),
@Unit nvarchar(4),
@Price decimal(10,2),
@TotalPrices decimal(10,2),
@CurrencyID INT,
@ChargingMethod nvarchar(8),
@ChargingType nvarchar(8),
@ProducingArea nvarchar(10),
@LOTNO nvarchar(10)
-- 新增
DECLARE CargoListNew_cur CURSOR FOR
SELECT SUM(PieceQuantity) AS SumPieceQuantity, SUM(LegalQuantity) AS SumLegalQuantity, SUM(GrossWeight) AS SumGrossWeight, SUM(NetWeight) AS SumNetWeight, SUM(Volume) AS SumVolume,
InDepotID, CargoNO,CustomerOrderID, Code_T, CargoName, TypeOrMarks, Sign, Price, TotalPrices, CurrencyID, ChargingMethod, ChargingType, ProducingArea, LOTNO
FROM BW_CannibalizeList
WHERE (CannibalizeID = @CannibalizeID)
GROUP BY InDepotID, CargoNO, CustomerOrderID, Code_T, CargoName, TypeOrMarks, Sign, Price, TotalPrices, CurrencyID, ChargingMethod, ChargingType, ProducingArea, LOTNO
OPEN CargoListNew_cur
FETCH NEXT FROM CargoListNew_cur
INTO @PieceQuantity, @LegalQuantity, @GrossWeight, @NetWeight, @Volume, @InDepotID,@CargoNO,@CustomerOrderID, @Code_T, @CargoName, @TypeOrMarks, @Sign, @Price, @TotalPrices, @CurrencyID, @ChargingMethod, @ChargingType, @ProducingArea, @LOTNO
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT BW_CO_Cargo(CargoNO, DepotID,CustomerOrderID, Code_T, CargoName, TypeOrMarks, PieceQuantity, Sign, LegalQuantity, Unit, NetWeight, GrossWeight,Volume, Price, TotalPrices, CurrencyID, ChargingMethod, ChargingType, ProducingArea,LOTNO)
VALUES(@CargoNO, @InDepotID,@CustomerOrderID, @Code_T, @CargoName, @TypeOrMarks, @PieceQuantity, @Sign, @LegalQuantity, @Unit, @NetWeight, @GrossWeight,@Volume, @Price, @TotalPrices, @CurrencyID, @ChargingMethod, @ChargingType, @ProducingArea,@LOTNO)
FETCH NEXT FROM CargoListNew_cur
INTO @PieceQuantity, @LegalQuantity, @GrossWeight, @NetWeight, @Volume, @InDepotID,@CargoNO,@CustomerOrderID, @Code_T, @CargoName, @TypeOrMarks, @Sign, @Price, @TotalPrices, @CurrencyID, @ChargingMethod, @ChargingType, @ProducingArea, @LOTNO
END
CLOSE CargoListNew_cur
DEALLOCATE CargoListNew_cur
--删除,更新
DECLARE @OldPieceQuantity INT,
@OldLegalQuantity INT,
@OldGrossWeight DECIMAL(10,2),
@OldNetWeight DECIMAL(10,2),
@OldVolume DECIMAL(10,2)
DECLARE CargoListDelOrUp_cur CURSOR FOR
SELECT SUM(PieceQuantity) AS SumPieceQuantity, SUM(LegalQuantity) AS SumLegalQuantity, SUM(GrossWeight) AS SumGrossWeight, SUM(NetWeight) AS SumNetWeight, SUM(Volume) AS SumVolume, OutDepotID,InCargoID
FROM BW_CannibalizeList
WHERE (CannibalizeID = @CannibalizeID)
GROUP BY OutDepotID, InCargoID
OPEN CargoListDelOrUp_cur
FETCH NEXT FROM CargoListDelOrUp_cur
INTO @PieceQuantity, @LegalQuantity, @GrossWeight, @NetWeight, @Volume, @OutDepotID,@InCargoID
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @OldPieceQuantity = PieceQuantity,@OldLegalQuantity = LegalQuantity,@OldNetWeight = NetWeight, @OldGrossWeight= GrossWeight,@OldVolume = Volume
FROM BW_CO_Cargo
WHERE CargoID = @InCargoID
IF @LegalQuantity = @OldLegalQuantity -- @PieceQuantity = @OldPieceQuantity AND @LegalQuantity = @OldLegalQuantity AND @GrossWeight = @OldGrossWeight AND @NetWeight = @OldNetWeight AND @Volume = @OldVolume
BEGIN
DELETE FROM BW_CO_Cargo
WHERE CargoID = @InCargoID
END
ELSE
BEGIN
IF @LegalQuantity < @OldLegalQuantity -- @PieceQuantity < @OldPieceQuantity AND @LegalQuantity < @OldLegalQuantity AND @GrossWeight < @OldGrossWeight AND @NetWeight < @OldNetWeight AND @Volume < @OldVolume
UPDATE BW_CO_Cargo
SET PieceQuantity = @OldPieceQuantity - @PieceQuantity, LegalQuantity = @OldLegalQuantity - @LegalQuantity, GrossWeight = @OldGrossWeight - @GrossWeight, NetWeight = @OldNetWeight - @NetWeight, Volume = @OldVolume - @Volume
WHERE CargoID = @InCargoID
END
FETCH NEXT FROM CargoListDelOrUp_cur
INTO @PieceQuantity, @LegalQuantity, @GrossWeight, @NetWeight, @Volume, @OutDepotID,@InCargoID
END
CLOSE CargoListDelOrUp_cur
DEALLOCATE CargoListDelOrUp_cur
RETURN