一次触发即可更新

一次触发即可更新

本文介绍了插入后,一次触发即可更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我编写了一个触发器,当您在特定表中更新或插入任何记录时会触发该触发器.

现在,我想在将记录插入表中时在触发器中执行一个代码块.
并且我想在更新表中的任何记录时在触发器中执行另一个代码块.

如何在触发器中放置条件.


问候,

Hi

I have written a trigger which gets fired when you update or insert any record in a particular table.

Now, I want to execute one block of code in the trigger when i insert a record into the table.
and i want to execute another block of code in the trigger when i update any record in the table.

How can i put a condition in the trigger.


Regards,

推荐答案


CREATE TRIGGER dbo.TableName_IUD
ON dbo.TableName
AFTER INSERT, UPDATE, DELETE
AS
BEGIN
 SET NOCOUNT ON;

 DECLARE @action CHAR(8)
 IF COLUMNS_UPDATED() <> 0 -- delete or update?
 BEGIN
  IF EXISTS (SELECT * FROM deleted) -- updated cols + old rows means action=update       
    SET @action = 'UPDATE'
  ELSE
    SET @action = 'INSERT' -- updated columns and nothing deleted 
                           --means action=insert
 END
 ELSE -- delete     
 BEGIN
  SET @action = 'DELETE'
 END

END



谢谢



Thank you


这篇关于插入后,一次触发即可更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 17:23