表级触发和列级触发可以同时在一个触发器里定义吗?我做了一个,能生成,但执行有问题!代码入下:请高人指点!
create trigger tr_进货单
on 进货单
for update,delete,insert
as
begin
declare @sellid varchar(8)
declare @stockid varchar(8)
select @sellid=进货订单号 from inserted
select @stockid=仓库号 from inserted
if (select 审核 from deleted)='确定'
begin
print '操作失败!'
rollback
end
if update(审核)
if (select 审核 from inserted)= '确定' and (select 审核 from deleted)= null
begin
declare cur_订货清单 scroll cursor
for
select 材料号,单价,数量 from 进货清单 WHERE 进货订单号=@sellid
open cur_订货清单
declare @MaterialID varchar(8)
declare @Materialamount int
declare @newprice money
fetch next from cur_订货清单 into @MaterialID,@newprice,@Materialamount
while @@fetch_status=0
begin
if exists(select 材料号 from 库存信息 where 材料号=@MaterialID and 仓库号=@stockid)
begin
declare @tempmount int
declare @tempprice money
select @tempmount=库存量 from 库存信息 where 材料号=@MaterialID and 仓库号=@stockid
select @tempprice=平均单价 from 库存信息 where 材料号=@MaterialID and 仓库号=@stockid
update 库存信息 set 库存量=库存量+@Materialamount,平均单价=((@tempmount*@tempprice+@newprice*@Materialamount)/(@tempmount+@Materialamount)),最新进价=@newprice where 材料号=@MaterialID and 仓库号=@stockid
end
else
begin
insert 库存信息(材料号,仓库号,最新进价,平均单价,库存量)values(@MaterialID,@stockid,@newprice,@newprice,@Materialamount)
end
fetch next from cur_订货清单 INTO @MaterialID,@newprice,@Materialamount
end
close cur_订货清单
deallocate cur_订货清单
end
end
go