写了一个简单的触发器,发现不能处理多行操作,现分享下解决方案。
先建两张表
代码
create
table
tableOne
(
id int identity ( 1 , 1 ) primary key , -- 主键
state int not null -- 状态
)
create table tableTwo
(
id int not null ,
oldState int not null , -- 更新前的状态
newState int not null -- 更新后的状态
)
(
id int identity ( 1 , 1 ) primary key , -- 主键
state int not null -- 状态
)
create table tableTwo
(
id int not null ,
oldState int not null , -- 更新前的状态
newState int not null -- 更新后的状态
)
要求实现的效果:tableOne中每次更新state的值时,用触发器写入tableTwo中。
触发器如下:
测试:
--测试
--增加10条数据
declare @num int
set @num=10
while @num>0
begin
insert into tableOne( state) values(@num)
set @num=@num-1
end
select * from tableOne
--更新此10条数据,使他们的state与id一样
update tableOne set state=id
select * from tabletwo
好了。效果实现了。
转载于:https://www.cnblogs.com/xinjian/archive/2010/07/14/1777606.html