Ralf Hohoff

Autoincrement in SELECT-Statement

image_pdfimage_print
-- MS SQL 2005 und höher
 
SELECT
  ROW_NUMBER() OVER( ORDER BY SomeColumn ) AS 'rownumber'
, *
FROM YourTable
 
-- MS SQL 2000
SELECT
  IDENTITY(INT, 1,1) AS Rank
‚ VALUE
INTO #Ranks
FROM YourTable WHERE 1=0
 
INSERT INTO #Ranks
SELECT SomeColumn FROM YourTable
ORDER BY SomeColumn
SELECT * FROM #Ranks
ORDER BY Ranks

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.