触发在SQL Server中插入,更新,删除库存数量

时间:2014-06-03 07:21:12

标签: sql-server

我有3个表,如下图所示: Table Figure

我的场景是插入,更新或删除tblGRDetail,然后更新tblSPStock中的StockQty字段。

这是我的触发器:

CREATE TRIGGER [dbo].[StockUpdate] on [dbo].[tblGRDetail]
FOR INSERT,DELETE,UPDATE
AS
BEGIN
IF EXISTS(SELECT * FROM inserted INNER JOIN tblGR ON inserted.GRNo=tblGR.GRNo WHERE tblGR.DealerCode=(SELECT DealerCode FROM tblSPStock WHERE DealerCode=tblGR.DealerCode) AND inserted.PartCode=(Select PartCode from tblSPStock WHERE PartCode=inserted.PartCode)) AND EXISTS (SELECT * FROM deleted INNER JOIN tblGR ON deleted.GRNo=tblGR.GRNo WHERE tblGR.DealerCode=(SELECT DealerCode FROM tblSPStock WHERE DealerCode=tblGR.DealerCode) AND deleted.PartCode=(Select PartCode from tblSPStock WHERE PartCode=deleted.PartCode))
BEGIN
UPDATE tblSPStock
SET StockQty=StockQty-(Select GRQty From deleted INNER JOIN tblGR ON deleted.GRNo=tblGR.GRNo WHERE tblSPStock.DealerCode=tblgr.DealerCode AND tblSPStock.PartCode=deleted.PartCode)+(Select GRQty From inserted INNER JOIN tblGR ON inserted.GRNo=tblGR.GRNo WHERE tblSPStock.DealerCode=tblGR.DealerCode AND tblSPStock.PartCode=inserted.PartCode)
WHERE tblSPStock.DealerCode=(select DealerCode from inserted INNER JOIN tblGR ON inserted.GRNo=tblgr.GRNo)
AND tblSPStock.PartCode=(select PartCode from inserted)
AND tblspstock.PartCode=(select PartCode from deleted)
END

IF EXISTS(SELECT * FROM inserted INNER JOIN tblGR ON inserted.GRNo=tblGR.GRNo WHERE tblGR.DealerCode=(SELECT DealerCode FROM tblSPStock WHERE DealerCode=tblGR.DealerCode) AND inserted.PartCode=(Select PartCode from tblSPStock WHERE PartCode=inserted.PartCode)) AND NOT EXISTS (SELECT * FROM deleted INNER JOIN tblGR ON deleted.GRNo=tblGR.GRNo WHERE tblGR.DealerCode=(SELECT DealerCode FROM tblSPStock WHERE DealerCode=tblGR.DealerCode) AND deleted.PartCode=(Select PartCode from tblSPStock WHERE PartCode=deleted.PartCode))
BEGIN
INSERT INTO tblSPStock (PartCode,
                        StockQty,
                        DealerCode)
SELECT PartCode, GRQty, tblGR.DealerCode from inserted inner join tblGR on inserted.GRNo=tblgr.GRNo
END

IF NOT EXISTS(SELECT * FROM inserted INNER JOIN tblGR ON inserted.GRNo=tblGR.GRNo WHERE tblGR.DealerCode=(SELECT DealerCode FROM tblSPStock WHERE DealerCode=tblGR.DealerCode) AND inserted.PartCode=(Select PartCode from tblSPStock WHERE PartCode=inserted.PartCode)) AND EXISTS (SELECT * FROM deleted INNER JOIN tblGR ON deleted.GRNo=tblGR.GRNo WHERE tblGR.DealerCode=(SELECT DealerCode FROM tblSPStock WHERE DealerCode=tblGR.DealerCode) AND deleted.PartCode=(Select PartCode from tblSPStock WHERE PartCode=deleted.PartCode))
BEGIN
UPDATE tblSPStock
SET StockQty=StockQty-(Select GRQty From deleted)
WHERE tblSPStock.DealerCode=(select DealerCode from deleted INNER JOIN tblGR ON deleted.GRNo=tblgr.GRNo)
AND tblspstock.PartCode=(select partcode from deleted)
END
END

当我插入tblGRDetail时,它显示以下错误: 子查询返回的值超过1。当子查询foolows =,!=,<,< =,>,> =或者子查询为ude时,不允许这样做

这里的任何一个人都对这个错误有所了解了!

==修订==

现在我在触发器中使用MERGE,但是当我删除tblGRDetail时,它不会更新StockQty。 但如果我删除INNER JOIN tblGR g ON d.GRNo = g.GRNo,那就OK了。

CREATE TRIGGER [dbo].[DeleteStock] on [dbo].[tblGRDetail]
FOR DELETE, UPDATE
AS
BEGIN
SET NOCOUNT ON;
    MERGE tblSPStock
    Using (
        SELECT
            d.PartCode,
            d.GRQty,
            g.DealerCode
        FROM deleted d INNER JOIN tblGR g ON d.GRNo=g.GRNo
    ) MergeData ON tblSPStock.PartCode= MergeData.PartCode
                   AND tblSPStock.DealerCode=MergeData.DealerCode
         WHEN MATCHED THEN 
        UPDATE SET
        tblSPStock.StockQty=tblSPStock.StockQty-MergeData.GRQty;
END

表示插入,更新正常,只有问题删除。

PLS。你的帮助是合适的。

2 个答案:

答案 0 :(得分:1)

如果您不想切换到使用索引视图,无论出于何种原因,我认为以下内容是合适的:

create trigger tblGRDetail_T_IUD on tblGRDetail
after insert,update,delete
as
    ;With Updates as (
        select
            COALESCE(i.PartCode,d.PartCode) as PartCode,
            DealerCode,
            COALESCE(i.GRQty,0) - COALESCE(d.GRQty,0) as Qty
        from
            (select PartCode,GRNo,SUM(GRQty) as GRQty from inserted
                   group by PartCode,GRNo) i
                full outer join
            (select PartCode,GRNo,SUM(GRQty) as GRQty from deleted
                   group by PartCode,GRNo) d
                on
                    i.PartCode = d.PartCode and
                    i.GRNo = d.GRNo
                inner join
            tblGR g
                on
                    g.GRNo = COALESCE(i.GRNo,d.GRNo)
    )
    update s
    set StockQty = StockQty + u.Qty
    from
        tblSPStock s
            inner join
        Updates u
            on
                s.PartCode = u.PartCode and
                s.DealerCode = u.DealerCode

如果GRNoPartCodetblGRDetail中实际上是唯一的,那么您可以取出在GROUP上执行inserted的内部子查询和deleted

答案 1 :(得分:0)

你试过了 - 啊 - 理解非常明确的错误信息?

这是一个提示 - 触发器不是“每行”,它们是按操作。

例如:

  

SET StockQty = StockQty-(选择GRQty From deleted)

当删除的行有多行时会发生什么情况,因为更新是针对多行的?像这样不合法。假设你插入和删除了多行。