在SELECT語句中,增加一個欄位,可以自動編號1, 2, 3
SELECT ROW_NUMBER()
OVER (ORDER BY id) AS rowNo, id, Name FROM
Table1
結果如下:
rowNo id Name
1 400 台中市
2 408 台中市南屯區
3 411 台中縣太平市
4 4xx xxxx
此方法僅適用SQL Server 2005以上版本。
方法一:利用暫存Table
SELECT identity(int, 1,1) AS rowId INTO #tbl1 FROM table1
SELECT * FROM #tbl1
方法二:Row_Number()
SELECT ROW_NUMBER() OVER (ORDER BY [Id] ASC) AS No, [Id], [Name] FROM Table1
方法一:利用暫存Table
SELECT identity(int, 1,1) AS rowId INTO #tbl1 FROM table1
SELECT * FROM #tbl1
方法二:Row_Number()
SELECT ROW_NUMBER() OVER (ORDER BY [Id] ASC) AS No, [Id], [Name] FROM Table1
沒有留言:
張貼留言