且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么时候以及为什么在SQL Server中使用触发器?

更新时间:2023-02-03 19:56:55

为什么不使用 ^ ]?


您可能想拿起一本有关SQL的书并阅读.
You might want to pick up a book on SQL and read it.
It would help you understand the basics of triggers.


如果u在2表中具有依赖项,而I表中的Change则需要其他表中的Apply触发器的更改,这将使II中的Change变为1.
If u have dependency in 2 table and Change in I table requires a Changes in other Apply trigger to table I which will make Changes in II one
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER TRIGGER [<Trigger Name>]
   ON  [dbo].[<Table name where Change Occurs>]
   FOR INSERT
AS
DECLARE @Inserted_ID INT
BEGIN
    SET @InsertedID  = (SELECT FK_ID FROM INSERTED)
    -- Target Table for Trigger Will Update Some Count in Other Table
   Update  TrnTarget -- Table Name
     SET testColName= testColName+1
    WHERE FK_ID = @Inserted_ID

END