34,591
社区成员
发帖
与我相关
我的任务
分享
select id= row_number() over(order by getdate()),* from Table
select id=identity(int,1,1) as id,* from tb
--不用临时表
select id=(select count(1) from tb b where b.A <= a.A),* from tb a
--用临时表
select id=identity(int,1,1),* into # from tb
select * from #
drop table #
IF EXISTS(SELECT * FROM SYSOBJECTS WHERE NAME = N'tbl') DROP TABLE tbl
Create table tbl(A VARCHAR(5),B VARCHAR(6))
INSERT INTO tbl
SELECT 'a1','b1' UNION ALL
SELECT 'a2','b2' UNION ALL
SELECT 'a3','b3' UNION ALL
SELECT 'a4','b4'
SELECT * FROM tbl
/* A B
a1 b1
a2 b2
a3 b3
a4 b4
*/
select A,B into #t from tbl
delete from tbl
alter table tbl add ID int identity(1,1)
INSERT INTO tbl(A,B) SELECT A,B FROM #t
DROP TABLE #t
SELECT ID,A,B FROM tbl
/*
1 a1 b1
2 a2 b2
3 a3 b3
4 a4 b4
*/
select row_number() over(order by A) as id,* from table
select row_number() over(order by A) as id,* from table
select id= row_number() over(order by getdate()),* from Table
select id=identity(int,1,1),* into #1 from tb
select * from #1
--(2)用row_number()select * from
(
select *,row_number(order by A) rank from tb
) TB
order by rank
select id=identity(int,1,1),* into # from tb
select * from #
drop table #