您的位置:首页 > 其它

带子查询的更新语句

2009-11-25 12:55 134 查看
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' 无效。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: