Select from x th row next y rows

,

This code will show rows from 17th and then next 3 ordered by name from table authors in database pubs. Table must have primary key and of course ordered column. You can change value 'from' in line 'SET ROWCOUNT 17' and value 'next' (how many rows)in line SELECT TOP 3. It is good to show exact range of ordered data. Customize this code for your needs.

/*
Ivica Masar
pso@vip.hr
*/
USE pubs
DECLARE @var1 VARCHAR(12)
DECLARE @var2 VARCHAR(30)
SET ROWCOUNT 17
SELECT @var1 = au_id, @var2 = au_lname FROM authors ORDER BY au_lname, au_id
SET ROWCOUNT 0
SELECT TOP 3 * FROM authors 
WHERE  (au_id >= @var1 AND au_lname=@var2) OR ( au_lname>@var2)
ORDER BY  au_lname, au_id

Rate

Share

Share

Rate