SQL Server 페이지 조회 저장 프로시저 코드

 
CREATE PROCEDURE [dbo].[up_Pager]
@table varchar(2000), --
@col varchar(50), --
@orderby bit, -- ,0- ,1-
@collist varchar(800),-- ,*
@pagesize int, --
@page int, --
@condition varchar(800) --
AS
DECLARE @sql nvarchar(4000),@where1 varchar(800),@where2 varchar(800),
@total_Item int,@total_Page int
IF @condition is null or rtrim(@condition)=''
BEGIN--
SET @where1=' WHERE '
SET @where2=' '
END
ELSE
BEGIN--
SET @where1=' WHERE ('+@condition+') AND '--
SET @where2=' WHERE ('+@condition+') '--
END
SET @sql='SELECT @total_Item=CEILING((COUNT(*)+0.0)'+') FROM '+@table+ @where2
EXEC sp_executesql @sql,N'@total_Item int OUTPUT',@total_Item OUTPUT --
set @total_Page = Ceiling((@total_Item+0.0)/@pagesize) --

IF @orderby=0
SET @sql='SELECT TOP '+CAST(@pagesize AS varchar)+' '+@collist+
' , '+ CAST(@total_Item AS varchar) + ' as total_Item' +
' , '+CAST(@total_Page AS varchar) + ' as total_Page' +
' FROM mailto:'+@table+@where1+@col+'%3E(SELECT MAX('+@col+') '+
' FROM (SELECT TOP '+CAST(@pagesize*(@page-1) AS varchar)+' '+
@col+' FROM '+@table+@where2+'ORDER BY '+@col+') t) ORDER BY '+@col
ELSE
SET @sql='SELECT TOP '+CAST(@pagesize AS varchar)+' '+@collist+
' , '+ CAST(@total_Item AS varchar) + ' as total_Item' +
' , '+CAST(@total_Page AS varchar) + ' as total_Page' +
' FROM mailto:'+@table+@where1+@col+'%3C(select MIN('+@col+') '+
' FROM (SELECT TOP '+CAST(@pagesize*(@page-1) AS varchar)+' '+
@col+' FROM '+@table+@where2+'ORDER BY '+@col+' DESC) t) ORDER BY '+
@col+' DESC'
IF @page=1--
SET @sql='SELECT TOP '+CAST(@pagesize AS varchar)+' '+@collist+
' , '+ CAST(@total_Item AS varchar) + ' as total_Item' +
' , '+CAST(@total_Page AS varchar) + ' as total_Page' +
' FROM '+@table+
@where2+'ORDER BY '+@col+CASE @orderby WHEN 0 THEN '' ELSE ' DESC' END
--print @sql
EXEC(@sql)
SQL 에서 테스트(어떻게 사용 하 는 지 가르쳐 준다)
 
EXEC up_Pager '(SELECT * FROM )aa',' ',0- 1- ,' ', , ,' '
EXEC up_Pager '(SELECT * FROM T_Gather_Page)aa','SaveTime',1,'*',40,3,''

좋은 웹페이지 즐겨찾기