Detekcia párneho riadku vo výsledku
Na detekciu párneho riadka vo výsledku SELECTu môžeme použiť funkciu ROW_NUMBER a zároveň delenie "modulo" (celočíselné delenie so zvyškom) číslom 2. Ak je číslo riadka deliteľné číslom 2 bez zvyšku, vieme, že sa jedná o párny riadok.
Zdrojový kód:
/*Testovacie data*/
SELECT 'Peter' AS FirstName, 25 AS Age into #tmp
UNION ALL
SELECT 'Janko' AS FirstName, 30 AS Age
UNION ALL
SELECT 'Katka' AS FirstName, 28 AS Age
UNION ALL
SELECT 'Marek' AS FirstName, 15 AS Age
UNION ALL
SELECT 'Julia' AS FirstName, 22 AS AgeSELECT CASE WHEN (ROW_NUMBER() OVER (ORDER BY FirstName)) % 2 = 1 THEN 'N' ELSE 'Y' END as EvenLine, FirstName, Age FROM #tmp ORDER BY FirstNameDROP TABLE #tmp
Keywords: MS SQL, SELECT, EVEN ROW
SELECT 'Peter' AS FirstName, 25 AS Age into #tmp
UNION ALL
SELECT 'Janko' AS FirstName, 30 AS Age
UNION ALL
SELECT 'Katka' AS FirstName, 28 AS Age
UNION ALL
SELECT 'Marek' AS FirstName, 15 AS Age
UNION ALL
SELECT 'Julia' AS FirstName, 22 AS AgeSELECT CASE WHEN (ROW_NUMBER() OVER (ORDER BY FirstName)) % 2 = 1 THEN 'N' ELSE 'Y' END as EvenLine, FirstName, Age FROM #tmp ORDER BY FirstNameDROP TABLE #tmp