row_number
er den bedste tilgang, men da du kun ønsker en enkelt række, skal du sørge for at se på planen. Det kan vise sig bedre at identificere den ønskede række og derefter slutte sig til den oprindelige tabel for at hente yderligere kolonner.
WITH T1
AS (SELECT *,
ROW_NUMBER() OVER (ORDER BY number) AS RN
FROM master..spt_values)
SELECT name,
number,
type,
low,
high,
status
FROM T1
WHERE RN = 1000;
Giver
WITH T2
AS (SELECT number,
type,
name,
ROW_NUMBER() OVER (ORDER BY number) AS RN
FROM master..spt_values)
SELECT TOP 1 C.name,
C.number,
C.type,
C.low,
C.high,
C.status
FROM T2
CROSS APPLY (SELECT *
FROM master..spt_values v
WHERE v.number = T2.number
AND v.type = T2.type
AND ( v.name = T2.name
OR ( v.name IS NULL
AND T2.name IS NULL ) )) C
WHERE RN = 1000;
Giver