CREATE TRIGGER SyncInsert ON MyT WITH ENCRYPTION --加密,Trigger具体内容不可查看 AFTER INSERT --插入成功后,执行TRIGGER事件 AS BEGIN IF (ROWCOUNT_BIG() = 0) RETURN; INSERT INTO MyTBak (ID, Name, Age, Gender) SELECT ID, Name, Age, Gender /** *代表了Trigger作用的表对象(因为是INSERT动作,所以是这个INSERTED *参考:https://docs.microsoft.com/zh-cn/sql/relational-databases/triggers/use-the-inserted-and-deleted-tables?view=sql-server-ver15) */ FROM INSERTED END; GO INSERT INTO MyT(ID,Name,Age,Gender) Values(12,‘1‘,23,1); GO
需求逻辑:MyT表插入一条数据,自动在MyTBak表插入一条相同的数据;
截图如下所示: