Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this query in SQL Server 2008 r2
SQL
SELECT Row, id, id_2, car_num, photo, doc_type
FROM (SELECT ROW_NUMBER() OVER(ORDER BY id) AS Row, id, id_2, car_num, photo, doc_type
FROM Table_2 AS tbl) AS derivedtbl_1
WHERE (Row >= 1) AND (Row <= 5)

when I execute the query in vb net sqldataadapter it gets the 5 records but it doesn't move to next 5 records, it is a detail table with photo (archiving of documents as jpg from scanner) id_2 is the FK, after 12 records I get 'out of memory' and want to load only 5 records from Table_2 each time. I want to do paging.
Posted
Comments
[no name] 15-Oct-15 3:26am    
You have hard coded 1,5 in your query how it will move to next records. It should be dynamic means you have to pass value for next result set.

Like: WHERE (Row >= @NextRecord) AND (Row <= @NextRecord + 5)
E.F. Nijboer 15-Oct-15 3:34am    
Manas_Kumar, I think you can add this as answer.

1 solution

Create below procedure to implement paging:
SQL
CREATE PROCEDURE dbo.proc_Paging_TempTable
(
@Page int,
@RecsPerPage int
)
AS

-- The number of rows affected by the different commands
-- does not interest the application, so turn NOCOUNT ON
SET NOCOUNT ON;

-- Determine the first record and last record 
DECLARE @FirstRec int, @LastRec int;

SELECT @FirstRec = (@Page - 1) * @RecsPerPage
SELECT @LastRec = (@Page * @RecsPerPage + 1)
-- Return the set of paged records
WITH TempResult AS
(
SELECT ROW_NUMBER() OVER(ORDER BY id) AS RowNum, id, id_2, car_num, photo, doc_type
FROM Table_2
)

SELECT *
FROM TempResult
WHERE RowNum > @FirstRec 
AND RowNum < @LastRec;

GO

EXEC proc_Paging_TempTable 1, 5


You need to pass page no and no of page to stored procedure. In preceding procedure it is passing page no as 1 and no of records per page is 5. If you want next result set then you need to pass 2,5
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900