MSSQL触发器处理多行数据

2010072300092829.jpg 

写了一个简单的触发器,发现不能处理多行操作,现分享下解决方案。

先建两张表

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
    
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 -- 更新后的状态
)

 

要求实现的效果: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

猜你喜欢

转载自blog.csdn.net/weixin_34234823/article/details/93822274