update table_a set col_a = 'aaa' where table_a_key in (select top 10 table_a_key from table_a where col_a = 'b')
update table_a set col_a = 'aaa' where table_a_key in (select top 10 table_a_key from table_a where col_a = 'b')--------------------------------------- 这句只能对有关键字段的表起作用,没有关键字段的就不行了
楼上的语句调试过没有啊?不要想当然! 我帮你调试了一下有错误 我是这样写的(以PUBS为例,将前5名住在加洲的作者的地区都该为jjh,这样应该符合楼主的意思吧)update authors set state = 'jjh' where in (select top 5 state from authors where state = 'ca') 但是实际上是有错误的,请看!!Server: Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'in'.
update authors set state = 'jjh' where in (select top 5 state from authors where state = 'ca') 但是实际上是有错误的,请看!! -- 在 where 和 in 之间 应该加上 state
update authors set state = 'jjh' where in (select top 5 state from authors where state = 'ca') 但是实际上是有错误的,请看!! -- 在 where 和 in 之间 应该加上 state 不光是这点,还应该按主键来区分, update authors set state = 'jjh' where 主键 in (select top 5 主键 from authors where state = 'ca')
不行啊,大哥们,请看这句 update authors set state = 'qq' where state in (select top 5 state from authors where state = 'ca')(23 row(s) affected) 我又用语句查了一下 select au_id,state, au_lname from authors 成功是成功了,但是不是改了前5条 au_id state au_lname ----------- ----- ---------------------------------------- 172-32-1176 qq white 213-46-8915 qq Green 238-95-7766 qq Carson 267-41-2394 qq O'Leary 274-80-9391 qq Straight 341-22-1782 qq Smith 409-56-7008 qq Bennet 427-17-2319 qq Dull 472-27-2349 qq Gringlesby 486-29-1786 qq Locksley 527-72-3246 qq Greene 648-92-1872 qq Blotchet-Halls 672-71-3249 qq Yokomoto 712-45-1867 qq del Castillo 722-51-5454 qq DeFrance 724-08-9931 qq Stringer 724-80-9391 qq MacFeather 756-30-7391 qq Karsen 807-91-6654 qq Panteley 846-92-7186 qq Hunter 893-72-1158 qq McBadden 899-46-2035 qq Ringer 998-72-3567 qq Ringer(23 row(s) affected)
不行啊,大哥们,请看这句 update authors set state = 'qq' where state in (select top 5 state from authors where state = 'ca')-- 这样当然是不行的,应该要有关键字段才行; 如果在上面中 au_id 是关键字,可以这样: update authors set state = 'qq' where au_id in (select top 5 au_id from authors where state = 'ca')
哦,我看错了,应该执行这句 update authors set state = 'bb' where au_id in (select top 5 au_id from authors where state = 'qq')但是错了,都改为BB了
好了,搞定了 phantomMan厉害 正确的应该是这句 update authors set state = 'bb' where au_id in (select top 5 au_id from authors where state = 'qq')根据楼主的定义重新写一次 update 表格 set 需要修改的字段 = 'xxxx' where 表格的主键 in (select top 10 表格的主键 from 表格 where 需要修改的字段 = '符合的条件')
SET ROWCOUNT 使 Microsoft® SQL Server™ 在返回指定的行数之后停止处理查询。 语法 SET ROWCOUNT { number | @number_var } 参数 number | @number_var是在停止给定查询之前要处理的行数(整数)。注释 建议将当前使用 SET ROWCOUNT 的 DELETE、INSERT 和 UPDATE 语句重新编写为使用 TOP 语法。有关更多信息,请参见 DELETE、INSERT 或 UPDATE。对于在远程表和本地及远程分区视图上执行的 INSERT、UPDATE 和 DELETE 语句,忽略 SET ROWCOUNT 选项设置。若要关闭该选项(以便返回所有的行),请将 SET ROWCOUNT 指定为 0。说明 设置 SET ROWCOUNT 选项将使大多数 Transact-SQL 语句在已受指定数目的行影响后停止处理。这包括触发器和 INSERT、UPDATE 及 DELETE 等数据修改语句。ROWCOUNT 选项对动态游标无效,但限制键集的行集和不感知游标。使用该选项时应谨慎,它主要与 SELECT 语句一起使用。 如果行数的值较小,则 SET ROWCOUNT 替代 SELECT 语句 TOP 关键字。SET ROWCOUNT 的设置是在执行或运行时设置,而不是在分析时设置。权限 SET ROWCOUNT 权限默认授予所有用户。示例 SET ROWCOUNT 在指定的行数后停止处理。在下例中,注意有 x 行满足预付款少于或等于 $5,000 的条件;但是,从更新所返回的行数中可以看出并非所有的行都得到处理。ROWCOUNT 影响所有的 Transact-SQL 语句。USE pubs GO SELECT count(*) AS Cnt FROM titles WHERE advance >= 5000 GO下面是结果集:Cnt ----------- 11 (1 row(s) affected)现在,将 ROWCOUNT 设置为 4,并更新预付款等于或大于 $5,000 的所有行。-- SET ROWCOUNT to 4. SET ROWCOUNT 4 GO UPDATE titles SET advance = 5000 WHERE advance >= 5000 GO
SET ROWCOUNT 4 GO update table_a set col_a='aaa' where col_a='b' GO
set rowcount 属于mssql专有的吧
update shop_xs set quantity=quantity+1 where id in (select top 2 id from shop_jb where id<5)
update tabel_a set col_a='aaa' from (select top n * from table_a where col_a='b') as t1 where table_a.id=t1.id
你看这样行不行update tabel_a set col_a='aaa' from (select top n * from table_a where col_a='b') as t1 where table_a.id=t1.id
declare @n int set @n=10 --前10条记录 set rowcount @n update table_a set col_a='aaa' where col_a='b' set rowcount 0 这样就可以啦,但是遇到要从第2笔到6笔这一共5笔,用set rowcount @n也是不管用,没有主键实在是很难做事,你是查询还好,不管什么情况,反正top都可以(也必须有pk配合),万一你有所有栏位都一样,系统如何区分你要更新那一笔,所以加上个主键才是明智的做法~
set col_a='aaa' where col_a='b'
set col_a='aaa'
set col_a='aaa' where col_a='b'
----------------
这个语法都是错误的,再查询分析器里面不能执行
set col_a='aaa' 老是写错~~~~
所以你需要先将数据有序化到一个临时表或者表变量。然后才能进行update。最后再更新回原表。
where table_a_key in (select top 10 table_a_key from table_a where col_a = 'b')
where table_a_key in (select top 10 table_a_key from table_a where col_a = 'b')---------------------------------------
这句只能对有关键字段的表起作用,没有关键字段的就不行了
我帮你调试了一下有错误
我是这样写的(以PUBS为例,将前5名住在加洲的作者的地区都该为jjh,这样应该符合楼主的意思吧)update authors set state = 'jjh' where in (select top 5 state from authors where state = 'ca')
但是实际上是有错误的,请看!!Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'in'.
但是实际上是有错误的,请看!!
--
在 where 和 in 之间 应该加上 state
但是实际上是有错误的,请看!!
--
在 where 和 in 之间 应该加上 state
不光是这点,还应该按主键来区分,
update authors set state = 'jjh' where 主键 in (select top 5 主键 from authors where state = 'ca')
update authors set state = 'qq' where state in (select top 5 state from authors where state = 'ca')(23 row(s) affected)
我又用语句查了一下
select au_id,state, au_lname from authors
成功是成功了,但是不是改了前5条
au_id state au_lname
----------- ----- ----------------------------------------
172-32-1176 qq white
213-46-8915 qq Green
238-95-7766 qq Carson
267-41-2394 qq O'Leary
274-80-9391 qq Straight
341-22-1782 qq Smith
409-56-7008 qq Bennet
427-17-2319 qq Dull
472-27-2349 qq Gringlesby
486-29-1786 qq Locksley
527-72-3246 qq Greene
648-92-1872 qq Blotchet-Halls
672-71-3249 qq Yokomoto
712-45-1867 qq del Castillo
722-51-5454 qq DeFrance
724-08-9931 qq Stringer
724-80-9391 qq MacFeather
756-30-7391 qq Karsen
807-91-6654 qq Panteley
846-92-7186 qq Hunter
893-72-1158 qq McBadden
899-46-2035 qq Ringer
998-72-3567 qq Ringer(23 row(s) affected)
update authors set state = 'qq' where state in (select top 5 state from authors where state = 'ca')--
这样当然是不行的,应该要有关键字段才行;
如果在上面中 au_id 是关键字,可以这样:
update authors set state = 'qq' where au_id in (select top 5 au_id from authors where state = 'ca')
update authors set state = 'bb' where au_id in (select top 5 au_id from authors where state = 'qq')但是错了,都改为BB了
phantomMan厉害
正确的应该是这句
update authors set state = 'bb' where au_id in (select top 5 au_id from authors where state = 'qq')根据楼主的定义重新写一次
update 表格 set 需要修改的字段 = 'xxxx' where 表格的主键 in (select top 10 表格的主键 from 表格 where 需要修改的字段 = '符合的条件')
出现这种问题,本来就有些BT,如果还没有主键的话,那可以肯定这软件是LJ
使 Microsoft® SQL Server™ 在返回指定的行数之后停止处理查询。 语法
SET ROWCOUNT { number | @number_var } 参数
number | @number_var是在停止给定查询之前要处理的行数(整数)。注释
建议将当前使用 SET ROWCOUNT 的 DELETE、INSERT 和 UPDATE 语句重新编写为使用 TOP 语法。有关更多信息,请参见 DELETE、INSERT 或 UPDATE。对于在远程表和本地及远程分区视图上执行的 INSERT、UPDATE 和 DELETE 语句,忽略 SET ROWCOUNT 选项设置。若要关闭该选项(以便返回所有的行),请将 SET ROWCOUNT 指定为 0。说明 设置 SET ROWCOUNT 选项将使大多数 Transact-SQL 语句在已受指定数目的行影响后停止处理。这包括触发器和 INSERT、UPDATE 及 DELETE 等数据修改语句。ROWCOUNT 选项对动态游标无效,但限制键集的行集和不感知游标。使用该选项时应谨慎,它主要与 SELECT 语句一起使用。
如果行数的值较小,则 SET ROWCOUNT 替代 SELECT 语句 TOP 关键字。SET ROWCOUNT 的设置是在执行或运行时设置,而不是在分析时设置。权限
SET ROWCOUNT 权限默认授予所有用户。示例
SET ROWCOUNT 在指定的行数后停止处理。在下例中,注意有 x 行满足预付款少于或等于 $5,000 的条件;但是,从更新所返回的行数中可以看出并非所有的行都得到处理。ROWCOUNT 影响所有的 Transact-SQL 语句。USE pubs
GO
SELECT count(*) AS Cnt
FROM titles
WHERE advance >= 5000
GO下面是结果集:Cnt
-----------
11 (1 row(s) affected)现在,将 ROWCOUNT 设置为 4,并更新预付款等于或大于 $5,000 的所有行。-- SET ROWCOUNT to 4.
SET ROWCOUNT 4
GO
UPDATE titles
SET advance = 5000
WHERE advance >= 5000
GO
GO
update table_a set col_a='aaa' where col_a='b'
GO
(select top 2 id from shop_jb where id<5)
from (select top n * from table_a where col_a='b') as t1
where table_a.id=t1.id
from (select top n * from table_a where col_a='b') as t1
where table_a.id=t1.id
换句话说就是如何判断两行如果内容都相同的话。
如果数据库本身都不支持,那么这些讨论都是不必要的。
set @n=10 --前10条记录
set rowcount @n
update table_a set col_a='aaa' where col_a='b'
set rowcount 0
不过楼上的方法也不错,可以试试。
回复人: vivianfdlpw() ( ) 信誉:100 2005-07-25 08:35:00 得分: 0
declare @n int
set @n=10 --前10条记录
set rowcount @n
update table_a set col_a='aaa' where col_a='b'
set rowcount 0
这样就可以啦,但是遇到要从第2笔到6笔这一共5笔,用set rowcount @n也是不管用,没有主键实在是很难做事,你是查询还好,不管什么情况,反正top都可以(也必须有pk配合),万一你有所有栏位都一样,系统如何区分你要更新那一笔,所以加上个主键才是明智的做法~