如何创建触发器insert、update、delete
3个回答
展开全部
create trigger tr_a
on a
for insert,update,delere as
begin
IF EXISTS(SELECT 1 FROM inserted) AND NOT EXISTS(SELECT 1 FROM deleted)
BEGIN
-- 插入触发.
INSERT INTO B
SELECT
inserted.a
FROM
inserted;
END;
IF EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted)
BEGIN
-- 更新触发.
DELETE FROM B WHERE a in ( SELECT a FROM deleted);
INSERT INTO B
SELECT
inserted.a
FROM
inserted;
END;
IF NOT EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted)
BEGIN
-- 删除触发
DELETE FROM B WHERE a in ( SELECT a FROM deleted);
END;
end
on a
for insert,update,delere as
begin
IF EXISTS(SELECT 1 FROM inserted) AND NOT EXISTS(SELECT 1 FROM deleted)
BEGIN
-- 插入触发.
INSERT INTO B
SELECT
inserted.a
FROM
inserted;
END;
IF EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted)
BEGIN
-- 更新触发.
DELETE FROM B WHERE a in ( SELECT a FROM deleted);
INSERT INTO B
SELECT
inserted.a
FROM
inserted;
END;
IF NOT EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted)
BEGIN
-- 删除触发
DELETE FROM B WHERE a in ( SELECT a FROM deleted);
END;
end
参考资料: http://hi.baidu.com/wangzhiqing999/blog/item/6a325cbb7601cf1f18d81f60.html
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
触发器触发的条件有 insert,update,delete ,我想要一个例子 在一个表建立一create trigger tr_a on a for insert,update,delere as begin IF EXISTS
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
还是找一个软件高手给指点一下吧.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询