create table [tb]([LINE] sql_variant,[ITEMID] varchar(3),[ID] int)
insert [tb]
select null,'001',1 union all
select null,'001',2 union all
select null,'001',3 union all
select null,'002',4 union all
select null,'002',5 union all
select null,'002',6
update tb set line=0
--正确写法1
update a set
line = (select count(*) from tb
where itemid=a.itemid and id <= a.id)
from tb as a
-------------------------------------
--正确写法2
update tb
set line=(select count(*) from tb where itemid=a.itemid and id<=a.id)
from tb a
--------------------------------------------------------------------
--错误写法
update tb
set line=(select count(*)+1 from a where itemid=a.itemid and id<a.id)--select count(*) from tb
from tb a
--服务器: 消息 208,级别 16,状态 1,行 1
--对象名 'a' 无效。