以下查询有效:
update top(1) ShipBillInfo
set shipfirstname='kkk'
where CustomerId='134';
但是如果我尝试通过某些Id订购它会显示错误:例如:
update top(1) ShipBillInfo
set shipfirstname='kkk'
where CustomerId='134'
order by
OredrGUID desc;
答案 0 :(得分:31)
With cte as (
select top(1) shipfirtsname
From ShipBillInfo
where CustomerId='134'
order by OredrGUID desc)
Update cte set shipfirstname='abc';
答案 1 :(得分:22)
你为什么不这样做:
update ShipBillInfo
set shipfirstname='kkk'
where OrderGUID = (select top (1) OrderGUID
from ShipBillInfo
where CustomerId = 134
order by OredrGUID desc )
答案 2 :(得分:6)
对于线程安全解决方案,所提出的解决方案都不适用于我(某些行在同时执行时多次更新)。
这有效:
class ImageListFilter(admin.SimpleListFilter):
title = _('Has photo')
parameter_name = 'has_photo'
def lookups(self, request, model_admin):
return (
('yes', _('Yes')),
('no', _('No')),
)
def queryset(self, request, queryset):
if self.value() == 'yes':
return queryset.filter(image__isnull=False).exclude(image='')
if self.value() == 'no':
return queryset.filter(Q(image__isnull=True) | Q(image__exact=''))
class UserAdmin(admin.ModelAdmin):
list_filter = [ImageListFilter]
如果您想要返回更新项目的某些列,可以将其放入更新语句中:UPDATE Account
SET sg_status = 'A'
WHERE AccountId =
(
SELECT TOP 1 AccountId
FROM Account WITH (UPDLOCK) --this makes it thread safe
ORDER BY CreationDate
)
(OUTPUT INSERTED.AccountId
和SET
之间)