求SQL语句。。。。。。。。。。。

Qxlyh 2006-07-22 09:25:43
一个表:table3
三个字段:ID,Title,ClassID

我想用ORDER BY NEWID() 随机选取N条,将这N条的ClassID更新为1,请问SQL语句应怎么写,十分感谢。
...全文
115 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Qxlyh 2006-07-22
  • 打赏
  • 举报
回复
5555....
爱你
paoluo 2006-07-22
  • 打赏
  • 举报
回复
如果不固定,可以用動態語句


Create Table table3
(ID Int Identity(1,1),
Title Varchar(10),
ClassID Int)
Insert table3 Select 'aa',0
Union All Select 'bb',2
Union All Select 'cc',3
Union All Select 'dd',4
Union All Select 'ee',5
GO
Declare @N Int
Set @N=3
EXEC('Update table3 Set classid=1 Where ID In(Select Top '+@N+' ID From table3 Order By NewID())')

Select * From table3
GO
Drop Table table3
--Result
/*
ID Title ClassID
1 aa 0
2 bb 2
3 cc 1
4 dd 1
5 ee 1
*/

Qxlyh 2006-07-22
  • 打赏
  • 举报
回复
谢谢楼上两位
paoluo 2006-07-22
  • 打赏
  • 举报
回复
如果N是固定的


Update table3
Set classid=1
Where ID In(Select Top 3 ID From table3 Order By NewID())
九斤半 2006-07-22
  • 打赏
  • 举报
回复
将这N条的ClassID更新为1
刚才看才加1了,
update test set classid=1
where id in (select top 2 id from test order by newid())
九斤半 2006-07-22
  • 打赏
  • 举报
回复
create table test
(id int identity(1,1) not null,
title varchar(8000),
classid int)

insert test(title,classid)
select 'afdsaf',2
union all select 'asfdsafasf',9
union all select 'sdfafsaf',89
union all select 'sdfafsaf',3

select * from test

update test set classid=classid +1 where id in(
select top 2 id from test order by newid())

select * from test

drop table test
Qxlyh 2006-07-22
  • 打赏
  • 举报
回复
ID是自动编号,主键。
paoluo 2006-07-22
  • 打赏
  • 举报
回复
ID是不是關鍵字??

27,581

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧