linux系统选什么版本都一样吗
一个很好的方法,但是如果你授予代表权,那么它应该是第一个接受它的人。唯一的问题是插入它仍然是两个IO操作。
MS Sql2008 merge从SQL:2003标准介绍:
merge tablename with(HOLDLOCK) as target
using (values ('new value', 'different value'))
as source (field1, field2)
on target.idfield = 7
when matched then
update
set field1 = source.field1,
field2 = source.field2,
...
when not matched then
insert ( idfield, field1, field2, ... )
values ( 7, source.field1, source.field2, ... )
现在它只是一个IO操作,但是可怕的代码:-(
不要忘记交易。性能是好的,但简单(IF EXISTS ..)方法是非常危险的。
当多个线程将尝试执行插入或更新时,您可以轻松地获得主键违例。
@Beau Crawford&@Esteban提供的解决方案显示了一般想法,但容易出错。
为了避免死锁和PK违规,你可以使用这样的东西:
begin tran
if exists (select * from table with (updlock,serializable) where key = @key)
begin
update table set ...
where key = @key
end
else
begin
insert into table (key, ...)
values (@key, ...)
end
commit tran
要么
begin tran
update table with (serializable) set ...
where key = @key
if @@rowcount = 0
begin
insert into table (key, ...) values (@key,..)
end
commit tran
相似问题