SQL中触发器的代码写在哪里

 我来答
匿名用户
推荐于2016-02-14
展开全部

里分别盯山悔创建三个触发器,分别是插入、修改、删除时触发:
--插入触发器凯正
CREATE TRIGGER TR_INSERT ON B1
FOR INSERT
AS
IF (SELECT COUNT(*) FROM INSERTED)>=1
BEGIN
INSERT K2.dbo.B2(T3,T4) SELECT T1,T2 FROM INSERTED
END
GO
--删除触发器
CREATE TRIGGER TR_DELETE ON B1
FOR DELETE
AS
IF (SELECT COUNT(*) FROM DELETED)>=1
BEGIN
DELETE K2.dbo.B2 WHERE K2.dbo.B2.T3=(SELECT T1 FROM DELETED) AND K2.dbo.B2.T4=(SELECT T2 FROM DELETED)
END
GO
--修改触发器
CREATE TRIGGER TR_UPDATE ON B1
FOR UPDATE
AS
UPDATE K2.dbo.B2 SET T3=(SELECT T1 FROM INSERTED), T4=(SELECT T2 FROM INSERTED)
WHERE T3=(SELECT T1 FROM DELETED) AND T4=(SELECT T2 FROM DELETED)

GO
--测试数据
insert into B1 values('1','2')
select * from K1.dbo.B1
SELECT * FROM K2.DBO.B2

UPDATE B1 SET T1='4' WHERE T1='1'
select * from K1.dbo.B1
SELECT * FROM K2.DBO.B2

DELETE B1 WHERE T1='唯迅4'
select * from K1.dbo.B1
SELECT * FROM K2.DBO.B2

本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式