求SQL 语句

liyuncdc 2011-03-26 10:31:41
一个表T,有N个记录,表T里有字段A,要求就是把A update 成为从1 到 N,,数字来的
不用循环,,如何才能做到呢?
...全文
112 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
liang145 2011-03-28
  • 打赏
  • 举报
回复

update #tb2 set id2=t.num from
(select ROW_NUMBER() OVER (order by id2)as num,id from #tb2) as t
where t.id=#tb2.id
lijian8552 2011-03-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 acherat 的回复:]
SQL code

declare @i int
set @i = 0
update tb
set @i = @i + 1,a = @i

/*select * from tb*/


--2005 : row_number

update t
set t.a = b.rn
from tb t,(select rn = row_numbr() over (order b……
[/Quote]




正解!!!
oupizi 2011-03-26
  • 打赏
  • 举报
回复
[code=SQL]/****** 数据库自动备份并压缩同时删除过期压缩文件 ******/
USE Master
GO
/** 开启xp_cmdshell支持 **/
Exec sp_configure 'show advanced options', 1
Reconfigure with override
Exec sp_configure 'xp_cmdshell', 1
Reconfigure with override
Exec sp_configure 'show advanced options', 0
Reconfigure with override

/** 定义参数 **/
Declare

--备份文件存储路径
@FilePath Nvarchar(100),

--数据库备份格式 为 数据库名称_日期(日期格式:20110326).bak
--数据库名称
@DbName Nvarchar(100),

--数据库备份后缀
@FileName Nvarchar(100),

--被压缩文件名称
@BakFile Nvarchar(100),

--压缩文件名称
@RarFileName Nvarchar(100),

--rar文件存放路劲
@RarFilePath varchar(100),
@RarCmd Nvarchar(150),
@Str varchar(100),
@Dir varchar(100)

Set @FilePath = 'D:\Cr_DataBackup\'
Set @DbName = 'CR_YFTG_FQ12'
Set @FileName = convert(varchar(10),getdate(),112)
Set @RarFilePath = 'C:\Progra~1\WinRAR\RAR.exe'
set @BakFile=@FilePath+@DbName+'_'+@FileName+'.bak'
set @RarFileName=@FilePath+''+@DbName+'_'+@FileName+'.rar'
/** 定义完成 **/

/** 开始完整备份数据库 **/
Set @Str=@FilePath+@DbName+'_'+@FileName+'.bak'
BACKUP DATABASE @DbName TO DISK=@str
WITH RETAINDAYS=15,NOFORMAT,NOINIT,
NAME=N'完整备份',SKIP,NOREWIND,
NOUNLOAD,STATS=10
/** 备份完成 **/

/** 开始压缩新备份文件 **/
Set @RarCmd =@RarFilePath+' a -df -ep1 -m5 '+@RarFileName+' '+@BakFile
Exec master..xp_cmdshell @RarCmd
/** 参数说明 **/
/** a:添加文件到压缩文件 -df:压缩后删除文件 -ep1:从名称中排除基本目录 -m5:压缩级别为最大 **/
/** 压缩完成 **/

/** 删除过期的压缩文件 **/
Set @Dir='Del '+@FilePath+@DbName+'_'+convert(varchar(10),getdate()-4,112)+'.rar'
Exec master..xp_cmdshell @Dir
/** 删除结束 **/

/** 关闭xp_cmdshell支持 **/
Exec sp_configure 'show advanced options', 1
Reconfigure with override
Exec sp_configure 'xp_cmdshell', 1
Reconfigure with override
Exec sp_configure 'show advanced options', 0
Reconfigure with override
/** 过程完成 **/
[/code]
AcHerat 元老 2011-03-26
  • 打赏
  • 举报
回复

declare @i int
set @i = 0
update tb
set @i = @i + 1,a = @i

/*select * from tb*/


--2005 : row_number

update t
set t.a = b.rn
from tb t,(select rn = row_numbr() over (order by getdate()),* from tb)b
where t.[关联字段] = b.[关联字段]

--2000 : identity

select rn = identity(int,1,1),*
into #t
from tb

update t
set t.a = b.rn
from tb t,#t b
where t.[关联字段] = b.[关联字段]


--上边打错!
AcHerat 元老 2011-03-26
  • 打赏
  • 举报
回复

declare @i int
set @i = 0
update tb
set @i = @i + 1,a = @i

/*select * from tb*/


--2005 : row_number

update t
set a = t.id
from tb t,(select id = row_numbr() over (order by getdate()),* from tb)b
where t.[关联字段] = b.[关联字段]

--2000 : identity

select rn = identity(int,1,1),*
into #t
from tb

update t
set a = t.id
from tb t,#t b
where t.[关联字段] = b.[关联字段]
dawugui 2011-03-26
  • 打赏
  • 举报
回复
[Quote=引用楼主 liyuncdc 的回复:]
一个表T,有N个记录,表T里有字段A,要求就是把A update 成为从1 到 N,,数字来的
不用循环,,如何才能做到呢?
[/Quote]
最好给出完整的表结构,测试数据,计算方法和正确结果.否则耽搁的是你宝贵的时间。
如果有多表,表之间如何关联?


发帖注意事项
http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281
--小F-- 2011-03-26
  • 打赏
  • 举报
回复
update 
t
set
a=t.id
from
t,(select id=row_numbr()over(order by getdate()),* from t)b
where
t.关联字段=b.关联字段
  • 打赏
  • 举报
回复
简单一点吧


declare @i int
set @i=0
update tb
set @i=@i+1,
sn=@i

select * from tb
  • 打赏
  • 举报
回复
哦,上面的写错了
  • 打赏
  • 举报
回复
update t
set a=row_number()over order by(getdate())
dearbinge 2011-03-26
  • 打赏
  • 举报
回复

;WITH TEMP
AS
(
SELECT ROW_NUMBER() OVER(ORDER BY FID) AS NUM,* FROM TABLE1
)
UPDATE T
SET TB.A=T.NUM
FROM T
INNER JOIN TEMP T1
ON T.ID=T1.ID
jornye 2011-03-26
  • 打赏
  • 举报
回复
不知道数据库及数据库版本·
那就写个oracle的好了·
9i测试通过·
create table A
(
ID NUMBER,
NAME VARCHAR2(10)
);
insert into a values(2,'tom');
insert into a values(3,'tina');
commit;
select * from a;
update a set id = rownum;
commit;
select * from a;

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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