如何使触发器仅对特定SP做出反应

时间:2018-10-02 10:28:32

标签: sql sql-server

create procedure UpdateIte(@iID int,@qtt int)
as begin
insert into updateItem
values(@iID,@qtt,getdate())
end

create procedure releseItem(@eid int,@qty int,@itemId int)
AS BEGIN
declare @no int,@qt int

select @no=qty
from Stock
where itemID=@itemID

if(@no>@qty)
BEGIN

    update Stock
    set qty=qty-@qty
    where itemID=@itemID

    select @qt=qty
    from Stock
    where itemID=@itemID

    insert into release
    values(@eid,@itemId,@qty,getdate())
    if(@qt<=10)
    BEGIN
        print'Item needs to replace'
    END

    else
        print'Success'
END
else
BEGIN
    print'Not Enough items in stock'
END
END

当要使用员工ID更新发布表的发布项目时,我想创建触发器。当我创建触发器时,这两个过程都会触发。但是我希望它用releaseItem触发。我该怎么办?

创建触发器CheckQty     库存     更新     一开始     声明@qty int,@ emp int,@ q int,@ ItemId int

select @qty= qty, @emp=eid, @ItemId=ItemId
from inserted

select @q=qty
from Stock
where ItemId=@ItemId

if(@qty>0)
begin
    insert into releseItem
    values (@eid,@ItemId,@qty)
end
else
    rollback transaction
end

1 个答案:

答案 0 :(得分:0)

您可以按如下所示在触发器中执行存储过程。

create trigger CheckQty ON Stock for Update as begin declare @qty int, @emp int, @q int,@ItemId int
select @qty= qty, @emp=eid, @ItemId=ItemId
from inserted

select @q=qty
from Stock
where ItemId=@ItemId

if(@qty>0)
begin

    exec releseItem @eid, @ItemId, @qty

end
else
    rollback transaction
end