sqlserver 7.0中删除触发器的问题(50分)

  • 主题发起人 主题发起人 hbnew
  • 开始时间 开始时间
H

hbnew

Unregistered / Unconfirmed
GUEST, unregistred user!
以下是表ReturnCurDayDetail 的删除触发器的内容
当发出命令:delete from ReturnCurDayDetail 时
ReturnCurDayDetail 表中的记录全部删除,但ReturnCurDay
(需要触发删除)表中的记录却只能删除一条对应的记录(实际应是全部删除),
请高手指教!


CREATE TRIGGER TR_Delete ON dbo.ReturnCurDayDetail
FOR DELETE
AS
begin
---声明变量,用于定位
declare @ItemUnitCode char(10),@CounterCode char(20),@ItemType char(2),@ReturnType char(2),@UserId char(6),@ReturnDate datetime
--定义游标
declare cur_dele scroll cursor for select ItemUnitCode,CounterCode,ItemType,ReturnType,UserId ,Convert(char(10),ReturnDate,20) from deleted
--打开
open cur_dele
fetch cur_dele into @ItemUnitCode ,@CounterCode ,@ItemType ,@ReturnType ,@UserId ,@ReturnDate
while @@FETCH_STATUS=0
begin
select @ItemUnitCode=ItemUnitCode ,@CounterCode=CounterCode,@ItemType=ItemType , @ReturnType=ReturnType,@UserId=UserId,@ReturnDate=ReturnDate from deleted
delete from ReturnCurDay
where ItemUnitCode=@ItemUnitCode and CounterCode=@CounterCode and ItemType=@ItemType and ReturnType=@ReturnType and UserId=@UserId
and convert(char(10),ReturnDate,20)=Convert(char(10),@ReturnDate,20)
fetch cur_dele into @ItemUnitCode ,@CounterCode ,@ItemType ,@ReturnType ,@UserId ,@ReturnDate
end
close cur_dele
DEALLOCATE cur_dele
end
 
删除这一行试试:
select @ItemUnitCode=ItemUnitCode ,@CounterCode=CounterCode,@ItemType=ItemType , @ReturnType=ReturnType,@UserId=UserId,@ReturnDate=ReturnDate from deleted
 
删除
select @ItemUnitCode=ItemUnitCode ,@CounterCode=CounterCode,@ItemType=ItemType , @ReturnType=ReturnType,@UserId=UserId,@ReturnDate=ReturnDate from deleted
因为你做游标就是因为删除多条记录,
你再掉用上面语句只会一直是deleted表中的一条记录了
就是说你后面where过滤的是同一个值,当然只删一条记录呀
 
多人接受答案了。
 
后退
顶部