表A:col1 col2
a 0
b 0
c 0表B
col1需求:把表A的col1插入表B的col1,同时A的col2都更新为'1',而且要有数据更新的顺序,比如把col1值为a插入到表B中,这时值为a对应的col2才更新为1,如果是insert into B select col1 from A update a set col2 = 1,这样就没有意义了。由于表A与表B是在不同的服务器上,所以必须考虑到执行的效率问题,最好是类似与 insert into B select col1 from A,这种比较快捷的操作。谢谢各位大虾
a 0
b 0
c 0表B
col1需求:把表A的col1插入表B的col1,同时A的col2都更新为'1',而且要有数据更新的顺序,比如把col1值为a插入到表B中,这时值为a对应的col2才更新为1,如果是insert into B select col1 from A update a set col2 = 1,这样就没有意义了。由于表A与表B是在不同的服务器上,所以必须考虑到执行的效率问题,最好是类似与 insert into B select col1 from A,这种比较快捷的操作。谢谢各位大虾
alter insert
as
update a
set col2=1
from inserted i
where
i.col1=a.col1
alter insert
as
update a
set col2=1
from inserted i
where
i.col1=a.col1
if not object_id('a') is null
drop table a
Go
Create table a([col1] nvarchar(1),[col2] nvarchar(1))
Insert a
select N'a',N'0' union all
select N'b',N'0' union all
select N'c',N'0'
Go
create table b(Col1 nvarchar(1))
go
create trigger insert_b on b
after insert
as
update a
set col2=1
from inserted i
where
i.col1=a.col1
goinsert b select col1 from aselect * from a
col1 col2
---- ----
a 1
b 1
c 1(3 個資料列受到影響)
如:
insert b select col1 from a where col1='a'
update a set col2=1 where col1='a'
只需要操作A更新
或用一個表存儲發過的短信
以這個作為條件更新A