如何才能让identity的顺序与我的ORder by顺序相同?
原来数据
SortID Quality
-------------------------
3 B333B
4 A334B
7 A334A
8 K334B
9 K334A
10 K334K
13 W334B
18 W334A
19 W334K
我想得到如下的临时表
SortID Quality idd
-------------------------
3 B333B 1
4 A334B 2
7 A334A 3
8 K334B 4
9 K334A 5
10 K334K 6
13 W334B 7
18 W334A 8
19 W334K 9
用如下SQL语句得到的结果
SELECT d.SortID, d.Quality, idd=identity(int,1,1)
INTO #a
FROM dbo.Q_SaleQuoteDetail d
ORDER BY d.SortID
select * from #a Order by idd
drop table #a
SortID Quality idd
-------------------------
7 A334A 1
4 A334B 2
3 B333B 3
9 K334A 4
8 K334B 5
10 K334K 6
18 W334A 7
13 W334B 8
19 W334K 9
问题:如何才能让identity的顺序与我的ORder by顺序相同?
[解决办法]
--先创建临时表
create table #a(SortID int, Quality varchar(100), idd int identity(1,1))
insert into #a(SortID,Quality)
SELECT d.SortID, d.Quality
FROM dbo.Q_SaleQuoteDetail d
ORDER BY d.SortID
select * from #a Order by idd
drop table #a