我有这样的触发器 - 当具有这样名称的设备(总是一个元素)已经在表中时,它应该回滚。但触发器总是用 message 回滚事务such equipment already exist
。什么时候设备在桌子上,什么时候不在。问题是什么?
CREATE trigger [dbo].[ut_TEquipment]
on [dbo].[TEquipment]
for insert
as
begin
declare @var nvarchar(30)
select @var = name from inserted
print @var
if (@var in (select name from TEquipment))
begin
raiserror('Such equipment already exist',10,1)
rollback transaction
end
declare @amount int
declare @free int
select @amount = x.amount from inserted as x
select @free = y.free from TEquipWarehouse as y,inserted as x
where y.ideqwhs = x.ideqwhs
if (@free - @amount) <= 0
begin
raiserror('Free space in this warehouse is not enough!',10,1)
rollback transaction
end
else
begin
update TEquipWarehouse
set free = capacity - amount
from inserted as x
where idwhstype = x.ideqwhs
end
end