如今有表(account)有如下字段
id int --主键
username varchar(50)
pwd varchar(50)
loadCount int --默认为0
需求如下:
查询出前10条记录,按loadCount升序
select * from account order by loadCount asc
同时被查询出来的这10记录需要更新loadCount=loadCount+1
update account set loadCount=loadCount+1
以上操作如何同时进行?
由于并发高,所以禁止用update account set loadCount=loadCount+1 where id in (id1,id2...)
求高效的SQL语句或存储过程,谢谢大牛们!
------解决思路----------------------
你是即要查询的结果,又要更新,是吗?
UPDATE T
SET loadCount=loadCount+1
OUTPUT DELETED.*
FROM(
SELECT TOP 10 * FROM account
ORDER BY loadCount DESC
)T