SQL SERVER 2005 创建触发器
要创建一个触发器,对corp表的insert,update,delete都要触发,触发后将corp表的所有字段插入corp_bak表,并在最后加个字段op,insert时...
要创建一个触发器,对corp表的insert,update,delete 都要触发,触发后将corp表的所有字段插入corp_bak表,并在最后加个字段op,insert时op=1,update时op=2,delete时op=3,corp表字段设定为ID,MC,NUM
展开
2个回答
展开全部
Create Trigger Tr_ForCorp
for insert,update,delete
on Corp
as
begin
Declare @Inserted int,@Deleted int
Set @Inserted = 0
Set @Deleted = 0
Select @Inserted = count(*) from Inserted
Select @Deleted = count(*) from Deleted
if @Inserted > 0 and @Deleted = 0 ---说明是插入触发
insert into corp_bak select *,1from inserted
if @Inserted > 0 and @Deleted > 0 ---说明是更新触发
insert into corp_bak select *,2 from inserted
if @Inserted = 0 and @Deleted > 0 ---说明是删除触发
insert into corp_bak select *,3 from inserted
end;
for insert,update,delete
on Corp
as
begin
Declare @Inserted int,@Deleted int
Set @Inserted = 0
Set @Deleted = 0
Select @Inserted = count(*) from Inserted
Select @Deleted = count(*) from Deleted
if @Inserted > 0 and @Deleted = 0 ---说明是插入触发
insert into corp_bak select *,1from inserted
if @Inserted > 0 and @Deleted > 0 ---说明是更新触发
insert into corp_bak select *,2 from inserted
if @Inserted = 0 and @Deleted > 0 ---说明是删除触发
insert into corp_bak select *,3 from inserted
end;
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询