Stored procedure for custom paging

have solved the stored procedure but notice that the SrNo is not declare for use.

as the code below :
CREATE procedure sp_StudentDtl
(
    @strSearchText varchar(100),
    @intPageSize int,
    @intCurrentPage int,
    @intTotalRecords int output
)
AS
BEGIN
 
-- Author: <Chetankumar Akarte>
-- Description: <Return Records depending on @intPageSize and @intCurrentPage.>

SELECT @intTotalRecords=Count(*)
FROM tblStudent A
JOIN tblClass B ON A.ClassId=B.ClassId
WHERE sName like @strSearchText +'%' 

SELECT sName, sAddress,ClassName
FROM
(
	SELECT TOP (@intCurrentPage*@intPageSize)
		sName, sAddress,ClassName,
		RN=ROW_NUMBER() over (order by SrNo)
	FROM tblStudent A
	JOIN tblClass B ON A.ClassId=B.ClassId
	WHERE sName like @strSearchText +'%'
	order by SrNo
) X
WHERE RN > (@intCurrentPage-1)*@intPageSize
 
END

Open in new window

LVL 1
doramail05Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ephraim WangoyaCommented:
Its already ordered by srno, you dont have to repeat it
CREATE procedure sp_StudentDtl
(
    @strSearchText varchar(100),
    @intPageSize int,
    @intCurrentPage int,
    @intTotalRecords int output
)
AS
BEGIN
 
-- Author: <Chetankumar Akarte>
-- Description: <Return Records depending on @intPageSize and @intCurrentPage.>

SELECT @intTotalRecords=Count(*)
FROM tblStudent A
JOIN tblClass B ON A.ClassId=B.ClassId
WHERE sName like @strSearchText +'%' 

SELECT sName, sAddress,ClassName
FROM
(
	SELECT TOP (@intCurrentPage*@intPageSize)
		sName, sAddress,ClassName,
		ROW_NUMBER() over (order by SrNo) RN
	FROM tblStudent A
	JOIN tblClass B ON A.ClassId=B.ClassId
	WHERE sName like @strSearchText +'%'	
) X
WHERE RN > (@intCurrentPage-1)*@intPageSize
 
END

Open in new window

doramail05Author Commented:
Noticed that it should have declared
dwkorCommented:
As the side note, if you have a lot of data, consider to move sorting/Row_Number() to separate CTE.

Check this out: http://aboutsqlserver.com/2011/02/06/sunday-t-sql-tip-server-side-paging-with-microsoft-sql-server/
doramail05Author Commented:
by knowing that
it havent declared, or if so , so it be? -- > 
SrNo int IDENTITY not null,
Ephraim WangoyaCommented:

You can create it as an Identity column if you don't already have a primary key.
Otherwise youe you primary to sort or just use the sName
CREATE procedure sp_StudentDtl
(
    @strSearchText varchar(100),
    @intPageSize int,
    @intCurrentPage int,
    @intTotalRecords int output
)
AS
BEGIN
 
-- Author: <Chetankumar Akarte>
-- Description: <Return Records depending on @intPageSize and @intCurrentPage.>

SELECT @intTotalRecords=Count(*)
FROM tblStudent A
JOIN tblClass B ON A.ClassId=B.ClassId
WHERE sName like @strSearchText +'%' 

SELECT sName, sAddress,ClassName
FROM
(
	SELECT TOP (@intCurrentPage*@intPageSize)
		sName, sAddress,ClassName,
		ROW_NUMBER() over (order by sName) RN
	FROM tblStudent A
	JOIN tblClass B ON A.ClassId=B.ClassId
	WHERE sName like @strSearchText +'%'	
) X
WHERE RN > (@intCurrentPage-1)*@intPageSize
 
END

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.