Saturday, July 10, 2010

SQL Sever Paging Stored Procedure

CREATE PROCEDURE prc_MediaGallery_SELECT_All
@insPageNo AS INT,
@insPageSize AS INT,
@intTotalCount AS INT out
AS
BEGIN

SET NOCOUNT ON;

DECLARE @intPageCount INT

BEGIN TRY
SELECT @intTotalCount = COUNT(MgId)
FROM TheSikhTV.MediaGallery

SET @intPageCount = @intTotalCount/@insPageSize;
IF (@intTotalCount%@insPageSize<>0)
SET @intPageCount= @intPageCount+1;
IF (@insPageNo>@intPageCount)
SET @insPageNo=@intPageCount;

SELECT TOP(@insPageSize) *
FROM (
SELECT ROW_NUMBER() OVER ( ORDER BY CreatedDate DESC) AS RowNum,
MgId,MgImageName,MgImagePath,CONVERT (Varchar (20),CreatedDate,106)as CreatedDate1,ModifiedDate
FROM TheSikhTV.MediaGallery
) A
WHERE A.RowNum > (@insPageSize * (@insPageNo - 1));

RETURN 1;
END TRY
BEGIN CATCH
RETURN -1;
END CATCH

END

No comments: