SQL Server在触发器中获取更新列

时间:2014-11-02 11:01:17

标签: sql-server triggers

我有以下触发器:

ALTER trigger [dbo].[tr_test]
ON [dbo].[customers]
for insert, update, delete
as
    IF (@@ROWCOUNT  = 0)  return
    SET NOCOUNT ON

    SELECT *
    FROM INSERTED I JOIN DELETED D
    ON I.id_no = D.id_no
        AND EXISTS (SELECT I.*
                    EXCEPT
                    SELECT D.*)  

触发器返回连接,显然包括所有表的列,甚至是那些没有更改的列。

是否可以过滤掉未更改的列,从而仅返回实际更改的列?

1 个答案:

答案 0 :(得分:-1)

尝试这样的事情 -



CREATE trigger [dbo].[tr_test]
ON [dbo].[Grades]
for insert, update, delete
as
    IF (@@ROWCOUNT  = 0)  return
    SET NOCOUNT ON
   
create tablae #inserted (...)
  insert into #inserted
   SELECT *  
    from INSERTED
create tablae #deleted (...)
     insert into #deleted
    SELECT *  
    from deleted
  




然后使用此脚本



 SELECT *
        FROM #inserted
        WHERE NOT EXISTS (SELECT * from #deleted)