update TB_AreaUserDevice_Relation set OrderID = t.r from TB_AreaUserDevice_Relation rel inner join (select ROW_NUMBER()over ( partition by AreaUserCode order by AreauserCode ) r,* from TB_AreaUserDevice_Relation ) t on rel.ID = t.ID