带子查询的更新语句
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' 无效。
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' 无效。
相关文章推荐
- 几种更新(Update语句)查询的方法
- mysql 更新带子查询(注意)
- 更新(Update语句)查询的方法
- sq更新语句条件子查询中有分组统计的sql,导致错误1093
- Sql 多表查询更新语句
- oracle联合查询并更新一个表字段的sql语句
- android中常用的查询、插入、更新、删除等SQL语句以及SQLite数据类型
- 几种更新(Update语句)查询的方法
- Day53、表连接、合并结果集、组函数和分组、子查询、表操作(创建、删除、更新)、事务控制语句
- Access update语句 提示 "操作必须使用一个可更新的查询"Access update语句 提示 "操作必须使用一个可更新的查询"
- SQL常用查询语句(不定期更新)
- C#使用oledb连接excel执行Insert Into语句出现“操作必须使用一个可更新的查询”的解决办法
- MYSQL查询语句:根据成绩更新全部排名
- SQL语句集锦——数据库表查询、更新、删除等SQL语法
- oracle联合查询并更新一个表字段的sql语句
- 几种更新(Update语句)查询的方法
- php开发中sql语句拼接示例(插入、查询、更新)
- 记录Oracle查询SQL语句(不定时更新)。
- 步步为营VS 2008 + .NET 3.5(9) - DLINQ(LINQ to SQL)之执行SQL语句的添加、查询、更新和删除
- SQL语句集锦——数据库表查询、更新、删除等SQL语法