我有一个表已经填充了5000行表。
我有一个名为SEQN的专栏。
我想用行计数填充此列。
我正在使用:
Microsoft SQL Server Management Studio 9.00.4035.00 Microsoft Analysis Services客户端工具2005.090.4035.00 Microsoft数据访问组件(MDAC)6.1.7601.17514 Microsoft MSXML 3.0 4.0 5.0 6.0 Microsoft Internet Explorer 9.0.8112.16421 Microsoft .NET Framework 2.0.50727.5466 操作系统6.1.7601
先谢谢您的帮助。
SQLNewbie
答案 0 :(得分:0)
declare @Foo as Table ( FooId Int Identity, Sequence Int, Timestamp DateTime );
insert into @Foo ( Sequence, Timestamp ) values
( 42, '20010203 10:18:05' ), ( 18, '20100508 22:18:05' ), ( NULL, '19960316 19:00:00' );
select * from @Foo order by Sequence;
update Foo
set Sequence = S
from ( select Sequence, Row_Number() over ( order by Timestamp ) as S from @Foo ) as Foo;
select * from @Foo order by Sequence;
update Foo
set Sequence = S
from ( select Sequence, Row_Number() over ( order by Timestamp desc ) as S from @Foo ) as Foo;
select * from @Foo order by Sequence;