什么时候以及为什么在SQL Server中使用触发器?
问题描述:
大家好,
什么时候以及为什么在SQL Server中使用触发器?有人可以举一些合适的例子来解释它.
谢谢
Hi all,
When and why we use trigger in SQL Server ?Can anybody explain it with giving suitable examples.
Thanks
答
为什么不使用 ^ ]?
您可能想拿起一本有关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