tb1
A B C D E F (字段)
3 1 1 1 1 1 (数据)tb2
A B C D E F (字段)想要一个触发器能实现这样的效果。
当我更新tb1表中的数据时,将tb1中还未更改的数据插入到tb2表中去。
update tb set B=2 where A=3,最后2个表的数据为
tb1
A B C D E F (字段)
3 2 1 1 1 1 (数据)tb2
A B C D E F (字段)
3 1 1 1 1 1 (数据)
A B C D E F (字段)
3 1 1 1 1 1 (数据)tb2
A B C D E F (字段)想要一个触发器能实现这样的效果。
当我更新tb1表中的数据时,将tb1中还未更改的数据插入到tb2表中去。
update tb set B=2 where A=3,最后2个表的数据为
tb1
A B C D E F (字段)
3 2 1 1 1 1 (数据)tb2
A B C D E F (字段)
3 1 1 1 1 1 (数据)
create table tb1
(A int,B int,C int,D int,E int,F int)insert into tb1
select 3,1,1,1,1,1create table tb2
(A int,B int,C int,D int,E int,F int)
-- 建触发器
create trigger tr_tb1 on tb1
for update
as
begin
insert into tb2(A,B,C,D,E,F)
select A,B,C,D,E,F from deleted
end-- 执行更新
update tb1 set B=2 where A=3-- 结果
select * from tb1
/*
A B C D E F
----------- ----------- ----------- ----------- ----------- -----------
3 2 1 1 1 1(1 row(s) affected)
*/select * from tb2
/*
A B C D E F
----------- ----------- ----------- ----------- ----------- -----------
3 1 1 1 1 1(1 row(s) affected)
*/
create table tb1(a int,b int,c int,d int,e int,f int)insert tb1 select 3,1,1,1,1,1select * into tb2 from tb1 where 1<>1create trigger trig_wh on tb1 for update
as
declare @rows int
set @rows=@@rowcountif @rows=0
returnif @rows>0
begin insert into tb2 select * from deleted
end