if(object_id('sendCardTrigger','tr') is not null)
drop trigger sendCardTrigger
go
create trigger sendCardTrigger
on DispatchCarSendCard
for insert,update,delete
as
declare @typeCard nvarchar(50) ,@cardId nvarchar(50),@plateNumber nvarchar(50),@PersonId nvarchar(50), @ticketIds nvarchar(50),
@rowNumber int,@id nvarchar(50),@ticketId nvarchar(50),@types nvarchar(50),@customerId nvarchar(50),@customerName nvarchar(50)
,@materialId nvarchar(50),@material nvarchar(50),@amount float, @sDate nvarchar(50),@sTime nvarchar(50),@optUser nvarchar(50),@states int,
@S01 nvarchar(50),@S02 nvarchar(50),@S03 nvarchar(50),@S04 nvarchar(50),@S05 nvarchar(50),@S06 nvarchar(50),@S07 nvarchar(50),@S08 nvarchar(50),@S09 nvarchar(50),
@isBucket nvarchar(50),@buckets nvarchar(50),@bucketWeight nvarchar(50)
if exists(select 1 from inserted) and not exists( select 1 from deleted) --insert 操作
begin
select @typeCard=type,@cardId=id, @plateNumber=PlateNum,@PersonId=by4,@ticketIds=planid from inserted
if(@typeCard='原盐采购')
begin
---1.声明游标
declare cur Cursor scroll for select * from ticket where ticketId=@ticketIds
--2.打开游标
open cur
--3.定位游标第一行,
fetch first from cur into @rowNumber,@id,@ticketId,@types,@customerId,@customerName,@materialId,@material,@amount,@sDate,@sTime,@optUser,@states,
@S01,@S02,@S03,@S04,@S05,@S06,@S07,@S08,@S09,@isBucket,@buckets,@bucketWeight
--4. 关闭游标
close cur
--5. 释放游标
deallocate cur
insert into ticketVerify(rowNumber,id,ticketId,types,customerId,customerName,materialId,material,amount,sDate,sTime,optUser,states,S01
,S02,S03,S04,S05,S06,S07,S08,S09,isBucket,buckets,bucketWeight,cardId,plateNumber,PersonId,checkState)values(@rowNumber,@id,@ticketId,@types,
@customerId,@customerName,@materialId,@material,@amount,@sDate,@sTime,@optUser,@states,@S01,@S02,@S03,@S04,@S05,@S06,@S07,@S08,@S09,@isBucket,@buckets,
@bucketWeight,@cardId,@plateNumber,@PersonId,'0')
end
end
else if exists(select 1 from inserted) and exists(select 1 from deleted) --update 操作
begin
select @typeCard=type,@cardId=id, @plateNumber=PlateNum,@PersonId=by4 ,@ticketIds=planid from inserted
if(@typeCard='原盐采购')
begin
---1.声明游标
declare cur Cursor scroll for select * from ticket where ticketId=@ticketIds
--2.打开游标
open cur
--3.定位游标第一行,
fetch first from cur into @rowNumber,@id,@ticketId,@types,@customerId,@customerName,@materialId,@material,@amount,@sDate,@sTime,@optUser,@states,
@S01,@S02,@S03,@S04,@S05,@S06,@S07,@S08,@S09,@isBucket,@buckets,@bucketWeight
--4. 关闭游标
close cur
--5. 释放游标
deallocate cur
update ticketVerify set rowNumber=@rowNumber,id=@id,ticketId=@ticketId,types=@types,customerId=@customerId,
customerName=@customerName,materialId=@materialId,material=@material,amount=@amount,sDate=@sDate,sTime=@sTime,
optUser=@optUser,states=@states,S01=@S01,S02=@S02,S03=@S03,S04=@S04,S05=@S05,S06=@S06,S07=@S07,S08=@S08,S09=@S09,
isBucket=@isBucket,buckets=@buckets,bucketWeight=@bucketWeight,cardId=@cardId,
plateNumber=@plateNumber,PersonId=@PersonId,checkState='0' where cardId=@cardId
end
end
else if not exists(select 1 from inserted) and exists(select 1 from deleted) --delete 操作
begin
select @typeCard=type,@cardId=id, @plateNumber=PlateNum,@PersonId=by4 from deleted
if(@typeCard = '原盐采购')
begin
update ticketVerify set del='1' where cardId=@cardId
end
end