SQLSERVER Pager store 프로시저 페이지 별 저장 프로 세 스


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[Pagination]
@Page int = 1, --
@PageSize int = 10, -- ( )
@Table nvarchar(500), -- , SQL:(Select * From Tab Where ID>1000) Tab
@Field nvarchar(800) = '*', -- ,"," , "*"
@OrderBy nvarchar(100) = 'ID ASC', --
@Filter nvarchar(500), --
@MaxPage smallint output, -- -1 error, 0 false, maxpage true
@TotalRow int output, -- /* 2007-07-12 22:11:00 update */
@Descript varchar(100) output --
AS
BEGIN

-- =============================================
-- Author: Jimmy.Yu
-- Create date: 2007-5-11
-- Description: SQL 2005
-- =============================================

Set ROWCOUNT @PageSize;

Set @Descript = 'successful';
------------------- ----------------
IF LEN(RTRIM(LTRIM(@Table))) !> 0
Begin
Set @MaxPage = 0;
Set @Descript = 'table name is empty';
Return;
End

IF LEN(RTRIM(LTRIM(@OrderBy))) !> 0
Begin
Set @MaxPage = 0;
Set @Descript = 'order is empty';
Return;
End

IF ISNULL(@PageSize,0) <= 0
Begin
Set @MaxPage = 0;
Set @Descript = 'page size error';
Return;
End

IF ISNULL(@Page,0) <= 0
Begin
Set @MaxPage = 0;
Set @Descript = 'page error';
Return;
End
------------------- ----------------

Begin Try
-- SQL
Declare @SQL nvarchar(4000), @Portion nvarchar(4000);

Set @Portion = ' ROW_NUMBER() OVER (ORDER BY ' + @OrderBy + ') AS ROWNUM FROM ' + @Table;

Set @Portion = @Portion + (CASE WHEN LEN(@Filter) >= 1 THEN (' Where ' + @Filter + ') AS tab') ELSE (') AS tab') END);

Set @SQL = 'Select TOP(' + CAST(@PageSize AS nvarchar(8)) + ') ' + @Field + ' FROM (Select ' + @Field + ',' + @Portion;

Set @SQL = @SQL + ' Where tab.ROWNUM > ' + CAST((@Page-1)*@PageSize AS nvarchar(8));

-- SQL,
Execute(@SQL);
--------------------------------------------------------------------

-- SQL
Set @SQL = 'Set @Rows = (Select MAX(ROWNUM) FROM (Select' + @Portion + ')';

-- SQL,
Execute sp_executesql @SQL, N'@Rows int output', @TotalRow output;
Set @MaxPage = (CASE WHEN (@TotalRow % @PageSize)<>0 THEN (@TotalRow / @PageSize + 1) ELSE (@TotalRow / @PageSize) END);
End Try
Begin Catch
--
Set @MaxPage = -1;
Set @Descript = 'error line: ' + CAST(ERROR_LINE() AS varchar(8)) + ', error number: ' + CAST(ERROR_NUMBER() AS varchar(8)) + ', error message: ' + ERROR_MESSAGE();
Return;
End Catch;

--
Return;
END
해당 페이지 논리 에 적 힌 해당 저장 과정 을 호출 하 는 방법(C\#)

좋은 웹페이지 즐겨찾기