• 全部
  • 基础类
  • 应用实例
  • 新技术前沿

帮我看看哈!谢谢!

NET321 2008-03-17 08:35:23

我想改下这个存储过程,使得在删除操作后将表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

...全文
52 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
liu_ff123 2008-03-17
UPDATE dbo.BW_CannibalizeList set dbo.BW_CannibalizeList.InCargoID=@@IDENTITY where dbo.BW_CannibalizeList.CannibalizeID=@CannibalizeID

回复
NET321 2008-03-17
思路我明白,就是怎么操作不明白,以前没有用过存储过程。我知道用@@IDENTITY
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
在这里怎么写这个UPDATE 语句
UPDATE dbo.BW_CannibalizeList set dbo.BW_CannibalizeList.InCargoID="?" where dbo.BW_CannibalizeList.CannibalizeID=@CannibalizeID


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
回复
JL99000 2008-03-17
大致看了一下,
思路就是在插入的时候取到刚插入的CargoID值,可以通过@@identity,scope_identity(),IDENT_CURRENT('表名'),更新即可

回复
dawugui 2008-03-17
@@IDENTITY
返回最后插入的标识值。

语法
@@IDENTITY

返回类型
numeric

注释
在一条 INSERT、SELECT INTO 或大容量复制语句完成后,@@IDENTITY 中包含此语句产生的最后的标识值。若此语句没有影响任何有标识列的表,则 @@IDENTITY 返回 NULL。若插入了多个行,则会产生多个标识值,@@IDENTITY 返回最后产生的标识值。如果此语句激发一个或多个执行产生标识值的插入操作的触发器,则语句执行后立即调用 @@IDENTITY 将返回由触发器产生的最后的标识值。若 INSERT 或 SELECT INTO 语句失败或大容量复制失败,或事务被回滚,则 @@IDENTITY 值不会还原为以前的设置。

在返回插入到表的 @@IDENTITY 列的最后一个值方面,@@IDENTITY、SCOPE_IDENTITY 和 IDENT_CURRENT 函数类似。

@@IDENTITY 和 SCOPE_IDENTITY 将返回在当前会话的所有表中生成的最后一个标识值。但是,SCOPE_IDENTITY 只在当前作用域内返回值,而 @@IDENTITY 不限于特定的作用域。

IDENT_CURRENT 不受作用域和会话的限制,而受限于指定的表。IDENT_CURRENT 返回任何会话和任何作用域中为特定表生成的标识值。有关更多信息,请参见 IDENT_CURRENT。

示例
下面的示例向带有标识列的表中插入一行,并用 @@IDENTITY 显示在新行中使用的标识值。

INSERT INTO jobs (job_desc,min_lvl,max_lvl)
VALUES ('Accountant',12,125)
SELECT @@IDENTITY AS 'Identity'

select * from tb where 自增列 = @@IDENTITY 
回复
相关推荐
发帖
MS-SQL Server
创建于2007-09-28

3.3w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
帖子事件
创建了帖子
2008-03-17 08:35
社区公告
暂无公告