SQL update top1 row query

前端 未结 3 1779
迷失自我
迷失自我 2020-12-01 20:32

The query below is working:

update  top(1) ShipBillInfo 
set     shipfirstname=\'kkk\' 
where   CustomerId=\'134\';

but it is showing error

相关标签:
3条回答
  • 2020-12-01 21:11

    Thread Safe

    For a thread safe solution none of the proposed solutions worked for me (some rows got updated more than once when executed it at the same time).

    This worked:

    UPDATE Account 
    SET    sg_status = 'A'
    WHERE  AccountId = 
    (
        SELECT TOP 1 AccountId 
        FROM Account WITH (UPDLOCK) --this makes it thread safe
        ORDER  BY CreationDate 
    )
    

    If you want to return some column of the updated item you can put this in your update statement: OUTPUT INSERTED.AccountId (between the SET and WHERE)

    0 讨论(0)
  • 2020-12-01 21:18
    With cte as (
    select  top(1) shipfirtsname  
    From ShipBillInfo 
    where   CustomerId='134' 
    order by  OredrGUID desc)
    Update cte set shipfirstname='abc';
    
    0 讨论(0)
  • 2020-12-01 21:24

    why dont you do :

    update ShipBillInfo 
    set shipfirstname='kkk' 
    where OrderGUID = (select top (1) OrderGUID  
                       from ShipBillInfo 
                       where CustomerId = 134 
                       order by OredrGUID desc )
    
    0 讨论(0)
提交回复
热议问题