Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Returning Data results of query in batches based on passed variable Expand / Collapse
Author
Message
Posted Monday, October 18, 2010 11:31 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, February 08, 2013 1:52 PM
Points: 3, Visits: 91
I have a request to return 100 rows of data per batch based on a variable passed to the my query. How do I do this?

Do I load my results to a temp table with an added id and make them use the id as their clause to pick the batch of data they need? This seems unneeded as I am sure there must be a better way to pass them the correct data. Any help would be appreciated.
Post #1006441
Posted Tuesday, October 19, 2010 5:06 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, August 26, 2012 6:05 PM
Points: 82, Visits: 70
if the requirement was known, then your underlying table must have been designed in the way you say.[a numeric / identity column]. if not, the easiest way to go about is what you have mentioned.
Post #1006857
Posted Tuesday, October 19, 2010 6:37 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 5:28 PM
Points: 11,627, Visits: 27,692
here's one way to do it, using row number to select the batches of numbers;

declare @NumRows int
declare @i int

SET @NumRows = 10 --return 10 rows at a time
SET @i = 3 --i want the 3rd resultset, 30-40
SELECT * FROM (
SELECT
row_number() OVER(ORDER BY name) AS RW,
sysobjects.*
FROM sysobjects ) myAlias
WHERE myAlias.RW BETWEEN (@NumRows * @i)
AND ((@NumRows * @i)+ @NumRows)



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1006912
Posted Tuesday, October 19, 2010 11:45 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, February 08, 2013 1:52 PM
Points: 3, Visits: 91
Yeah the rownumber worked great! I am going to be using this for multiple uses now.
Post #1007195
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse