Print 'Executing GetProperties' --This will return the maximum number of rows there are in the first dataset select count(*) as TotalRecordsFound from property as SearchInfo; declare @lineage varchar(900) if @localeid <> 0 begin select @lineage = Lineage +'%' From Localetree where LocaleId = @localeid end else select @lineage = '%' SELECT * FROM ( SELECT Property.EntityId as PropertyId,Property.[Name] as [Name],BookEmail,BookTelephone, Property.Description,NTAPAccredited,ECOAccredited, PropertyType.Name as PropertyTypeName,Commisionable, ROW_NUMBER() OVER(ORDER BY PropertyType.<%OrderField%> ) as RowNum FROM Property, PropertyType Where (Property.PropertyTypeId = PropertyType.PropertyTypeId) AND exists (Select PropertyId from product where product.propertyid = property.entityid) ) as Property AND Property.LocaleId in ( select LocaleId where Locale.Lineage like @lineage ) WHERE RowNum > @startRowIndex AND RowNum <= (@startRowIndex + @recordsPerPage)