插入后,一次触发即可更新

编程入门 行业动态 更新时间:2024-10-27 13:22:38
本文介绍了插入后,一次触发即可更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嗨 我编写了一个触发器,当您在特定表中更新或插入任何记录时会触发该触发器. 现在,我想在将记录插入表中时在触发器中执行一个代码块. 并且我想在更新表中的任何记录时在触发器中执行另一个代码块. 如何在触发器中放置条件. 问候,

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,

推荐答案

您不需要放置条件元素. 创建两个触发器-一个用于插入,另一个用于更新. 只需根据您的要求选择FOR(INSERT | UPDATE) [ ^ ]. You dont need to put conditional elements. Create two triggers - one for insert and another for update. Just choose the FOR (INSERT | UPDATE) based on your requirement[^].

试试这个示例代码 Hi, Try this sample code 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

更多推荐

插入后,一次触发即可更新

本文发布于:2023-10-26 08:08:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529586.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!