MSSQL触发器处理多行数据 

 

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

先建两张表

代码
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
MSSQL触发器处理多行数据

--更新此10条数据,使他们的state与id一样
update tableOne set state=id

select * from tabletwo
MSSQL触发器处理多行数据


好了。效果实现了。

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-08
  • 2021-09-04
猜你喜欢
  • 2021-11-09
  • 2021-12-06
  • 2022-12-23
  • 2022-12-23
  • 2021-08-24
相关资源
相似解决方案