2016-12-02 58 views
-1

我要尋找一個在SQL一些幫助創建使用SQL Server中的標識列,我使用下面的查詢如何與聲明

with t AS 
(
    select 
     EmpID, mgrid, HierarchyLevel, Description 
    from 
     empdatatest 
) 
select * 
from t 
order by empid 

我想辦法讓表T有標識列

數據輸出應該像

ID EmpID mgrid HierarchyLevel Description 
------------------------------------------ 
1 201 7  1   Partner 
2 202 201  2   Senior Manager 
3 221 202  3   Manager 
4 343 221  4   employee 
5 534 221  4   employee 
6 552 221  4   employee 
+0

是不是EmpID已經是一個標識列? –

回答

3

使用ROW_NUMBER()

;With t As 
(
    Select Row_Number() Over (Order By EmpId) As ID, 
      EmpID, 
      mgrid, 
      HierarchyLevel, 
      Description 
    From empdatatest 
) 
Select * 
From t 
Order By empid;