sql 使用触发器如何update多条记录

--begin 创建tb1表
if (object_id('tb1', 'u') is not null)
    drop table tb1
go
create table tb1(id int,name varchar(10),state int default(0)) 
declare @n int 
set @n=0
while (@n<10)
begin
	insert into tb1 (id,name)values(@n,'A'+cast( @n as varchar(5)))
	set @n+=1;
end
select * into tb2 from tb1
select * from tb1
--end

--begin 创建触发器
if (object_id('DataSync', 'TR') is not null)
    drop trigger DataSync
go
create trigger DataSync
on tb1
for update
as 
begin 
update tb2 
set tb2.name=tb1.name
from inserted as tb1 where tb2.id=tb1.id 
end 

--end

--begin 测试
update tb1
set name='tb1_name'
where id in (1,2,3,5,6,7)
select * from tb1
select * from tb2
--end

参考:

SQL Update语句,更新表中的符合要求的多条记录,更新的值是从另一个表中调用的

url:http://bbs.csdn.net/topics/280054052

SQL Server 触发器

url: http://www.cnblogs.com/hoojo/archive/2011/07/20/2111316.html

你可能感兴趣的:(sql 使用触发器如何update多条记录)