SQL 2008 触发器中UPDATE、DELETE
现有A,B二个表,B中比A中多一KEY例,A和B表中数据都一样。A表字段A,B,C,D,E,F,G字段A主健,非空B表字段A,B,C,D,E,F,G,KEY字段A主健,非...
现有A,B二个表,B中比A中多一KEY例,A和B表中数据都一样。
A表字段 A,B,C,D,E,F,G 字段A主健,非空
B表字段 A,B,C,D,E,F,G,KEY 字段A主健,非空
现想做触发器完成以下功能
1,A表UPDATE时,B表同步且B.KEY = 2,
2,A表发生DELETE时 B.KEY = 3 展开
A表字段 A,B,C,D,E,F,G 字段A主健,非空
B表字段 A,B,C,D,E,F,G,KEY 字段A主健,非空
现想做触发器完成以下功能
1,A表UPDATE时,B表同步且B.KEY = 2,
2,A表发生DELETE时 B.KEY = 3 展开
2个回答
展开全部
1.CREATE TRIGGER TEST1
ON A FOR UPDATE
AS
DECLARE @A,@B,@C,@D,@E,@F,@G
SELECT @B=B,....@G=G FROM INSERTED WHERE A=@A
UPDATE B SET A=@A,,,,G=@G,[KEY] = 2 WHERE @A=A
GO
2.CREATE TRIGGER TEST2
ON A FOR DELETE
AS
declare @A
SELECT @A = A FROM DELETED
UPDATE B SET [KEY] = 3
GO
第一个我不知道能不能完成你的功能 没有测试
ON A FOR UPDATE
AS
DECLARE @A,@B,@C,@D,@E,@F,@G
SELECT @B=B,....@G=G FROM INSERTED WHERE A=@A
UPDATE B SET A=@A,,,,G=@G,[KEY] = 2 WHERE @A=A
GO
2.CREATE TRIGGER TEST2
ON A FOR DELETE
AS
declare @A
SELECT @A = A FROM DELETED
UPDATE B SET [KEY] = 3
GO
第一个我不知道能不能完成你的功能 没有测试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1、题
create trigger up_Record
on A表
after update
as
begin
declare @a varchar(50),@b varchar(50),@c varchar(50),@d varchar(50),@e varchar(50),@f varchar(50),@g varchar(50);
declare up_Record cursor for
select A from inserted;
fetch next from up_Record into @a,@b,@c,@d,@e,@f,@g;
while @@fetch_status=0
begin
update B表 set B=@b,C=@c,D=@d,E=@e,F=@f,G=@g,B表.KEY=2 where A=@a
fetch next from up_Record into @a,@b,@c,@d,@e,@f,@g;
end
Close up_Record
Defallocate up_Record
end
2、题
create trigger de_Record
on A表
after delete
as
begin
update B表 KEY=3 where A in (select A from deleted)
end
create trigger up_Record
on A表
after update
as
begin
declare @a varchar(50),@b varchar(50),@c varchar(50),@d varchar(50),@e varchar(50),@f varchar(50),@g varchar(50);
declare up_Record cursor for
select A from inserted;
fetch next from up_Record into @a,@b,@c,@d,@e,@f,@g;
while @@fetch_status=0
begin
update B表 set B=@b,C=@c,D=@d,E=@e,F=@f,G=@g,B表.KEY=2 where A=@a
fetch next from up_Record into @a,@b,@c,@d,@e,@f,@g;
end
Close up_Record
Defallocate up_Record
end
2、题
create trigger de_Record
on A表
after delete
as
begin
update B表 KEY=3 where A in (select A from deleted)
end
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询