用于使另一个表保持最新的SQL触发器

时间:2015-04-10 03:48:31

标签: sql-server tsql triggers

我之前没有使用过触发器,但它们似乎是解决这个问题的方法。

我想要一张包含两张表中最新记录的表:Item_MasterDev_Master - > Union_Master

我不理解的主要是插入和删除的'表'。如果我想更新单行(更新行时),如何访问插入的表列?

这是我到目前为止所做的:

ALTER TRIGGER [dbo].[DevMasterChangeTrigger]
ON [dbo].[DEV_MASTER]
AFTER INSERT, UPDATE, DELETE
AS
    DECLARE @UpdateType nvarchar(1)
    DECLARE @UpdatedDT datetime

    SELECT @UpdatedDT = CURRENT_TIMESTAMP

    SET NOCOUNT ON;

    IF EXISTS (SELECT * FROM inserted)
       IF EXISTS (SELECT * FROM deleted)
          SELECT @UpdateType = 'U'    -- Update Trigger
       ELSE
          SELECT @UpdateType = 'I'    -- Insert Trigger
    ELSE
       IF EXISTS (SELECT * FROM deleted)
          SELECT @UpdateType = 'D'    -- Delete Trigger
       ELSE
          SELECT @UpdateType = NULL;  -- Unknown Operation

    IF @UpdateType = 'I'
    BEGIN
        PRINT 'Insert'
    END

    IF @UpdateType = 'U'
    BEGIN
        UPDATE dbo.UNION_MASTER 
        SET PRDNO = inserted.PRDNO & 'abc' 
        WHERE dbo.DEV_MASTER.PRDNO = inserted.PRDNO
    END

    IF @UpdateType = 'D'
    BEGIN
        PRINT 'Delete'
    END

此外,还有任何关于将自己沉浸在MS SQL Server中的好书吗?

编辑:好的,对于任何偶然发现这篇文章的人,我都明白了。希望我没有错过导致问题的任何细微差别。删除部分对我来说有点困惑......

USE [abc]
GO
/****** Object:  Trigger [dbo].[DevMasterChangeTrigger]    Script Date: 10/04/15 12:43:43 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[DevMasterChangeTrigger]
ON [dbo].[DEV_MASTER]
AFTER INSERT, UPDATE, DELETE
AS
DECLARE @UpdateType nvarchar(1)
DECLARE @UpdatedDT datetime

SELECT @UpdatedDT = CURRENT_TIMESTAMP



IF EXISTS (SELECT * FROM inserted)
    IF EXISTS (SELECT * FROM deleted)
        SELECT @UpdateType = 'U'    -- Update Trigger
    ELSE
        SELECT @UpdateType = 'I'    -- Insert Trigger
ELSE
    IF EXISTS (SELECT * FROM deleted)
        SELECT @UpdateType = 'D'    -- Delete Trigger
    ELSE
        SELECT @UpdateType = NULL;  -- Unknown Operation

IF @UpdateType = 'I'
BEGIN
   INSERT INTO [UNION_MASTER]
   (PRDID, {Insert various fields here})
   SELECT [inserted].[PRDID],   [inserted].{Insert various fields here}
   FROM inserted 
END

IF @UpdateType = 'U'
BEGIN
    UPDATE [UNION_MASTER] 
    SET 
    [UNION_MASTER].[abcdfield]=[inserted].[abcdfield], {other fields here}
    FROM inserted
    WHERE [UNION_MASTER].[PRDNO]=inserted.prdno     
END

IF @UpdateType = 'D'
BEGIN
    declare @prdno varchar(50)
    SELECT @prdno= PRDNO FROM deleted
    DELETE 
    FROM UNION_MASTER 
    WHERE PRDNO = @prdno
END

1 个答案:

答案 0 :(得分:1)

简而言之,inserteddeleted表是可以在DML触发器中使用的特殊表。这些表包含基于执行触发器的DML操作的行。

inserted表包含为insert语句插入的行数据或update

时新修改的行

已删除的表包含在delete操作中删除的行或在update

的情况下已修改的旧数据

在你的触发器中,你可以尝试这样的事情

ALTER TRIGGER [dbo].[DevMasterChangeTrigger]
ON [dbo].[DEV_MASTER]
AFTER INSERT, UPDATE, DELETE
AS
    DECLARE @UpdateType nvarchar(1)
    DECLARE @UpdatedDT datetime

    SELECT @UpdatedDT = CURRENT_TIMESTAMP

    SET NOCOUNT ON;

    IF EXISTS(SELECT * FROM inserted) AND EXISTS(SELECT * FROM deleted)
          SELECT @UpdateType = 'U'    -- Update Trigger
       ELSE IF EXISTS(SELECT * FROM inserted)
          SELECT @UpdateType = 'I'    -- Insert Trigger
       ELSE IF EXISTS (SELECT * FROM deleted)
          SELECT @UpdateType = 'D'    -- Delete Trigger
       ELSE
          SELECT @UpdateType = NULL;  -- Unknown Operation

    IF @UpdateType = 'I'
    BEGIN
        PRINT 'Insert'
        // add appropriate logic here
    END

    IF @UpdateType = 'U'
    BEGIN
        UPDATE U dbo.UNION_MASTER 
        SET PRDNO = inserted.PRDNO & 'abc'
        FROM dbo.UNION_MASTER U
        inner join inserted i -- this table will have the rows affected from update statement for table DEV_MASTER
        on i.<UNION_MASTER_ID> = U.<UNION_MASTER_ID> -- USE appropriate joining condition
    END

    IF @UpdateType = 'D'
    BEGIN
        PRINT 'Delete'
        // add appropriate logic here
    END

参考链接

https://msdn.microsoft.com/en-us/library/ms191300.aspx

http://www.mssqltips.com/sqlservertip/2342/understanding-sql-server-inserted-and-deleted-tables-for-dml-triggers/