Tin*_*ina 31 sql sql-server sql-server-2008
以下查询正在运行:
update top(1) ShipBillInfo
set shipfirstname='kkk'
where CustomerId='134';
Run Code Online (Sandbox Code Playgroud)
但如果我尝试按某些Id订购,则显示错误:例如:
update top(1) ShipBillInfo
set shipfirstname='kkk'
where CustomerId='134'
order by
OredrGUID desc;
Run Code Online (Sandbox Code Playgroud)
Rem*_*anu 32
With cte as (
select top(1) shipfirtsname
From ShipBillInfo
where CustomerId='134'
order by OredrGUID desc)
Update cte set shipfirstname='abc';
Run Code Online (Sandbox Code Playgroud)
sto*_*ter 22
你为什么不这样做:
update ShipBillInfo
set shipfirstname='kkk'
where OrderGUID = (select top (1) OrderGUID
from ShipBillInfo
where CustomerId = 134
order by OredrGUID desc )
Run Code Online (Sandbox Code Playgroud)
对于线程安全解决方案,所提出的解决方案都不适用于我(某些行在同时执行时不止一次更新).
这有效:
UPDATE Account
SET sg_status = 'A'
WHERE AccountId =
(
SELECT TOP 1 AccountId
FROM Account WITH (UPDLOCK) --this makes it thread safe
ORDER BY CreationDate
)
Run Code Online (Sandbox Code Playgroud)
如果要返回更新项的某些列,可以将其放在更新语句中:( OUTPUT INSERTED.AccountId在SET和之间WHERE)
| 归档时间: |
|
| 查看次数: |
65771 次 |
| 最近记录: |