110,539
社区成员
发帖
与我相关
我的任务
分享
if not exists(select * from [table] where name=@name)
insert [table](id, name, cost) values (@id, @name, 1500)
else
update [table] set name='xxx1' where name=@name
如果 insert、update 有多条记录,就使用begin....end语句块。高并发最好是做存储过程
select top 1 1 from tablename where uniquecolumnkey='onekey'
因为高并发,所以其实你可以直接Insert,然后通过catch的具体异常判断你是不是唯一性约束造成的,如果是的话,则执行update[quote=引用 7 楼 sp1234 的回复:] 不要写 count(*) 统计,而应该写 exists 查询,只要判断是否存在记录就行了,不要统计记录数。并且t-sql会自动优化 exists 中的 select * 语句(并不返回任何字段),所以应该使用 select * from .....而不是某些人写的 select 1 from .....。 一个更新语句通常是这样的如果 insert、update 有多条记录,就使用begin....end语句块。if not exists(select * from [table] where name=@name) insert [table](id, name, cost) values (@id, @name, 1500) else update [table] set name='xxx1' where name=@name