存储过程分页
一
/*
存储过程分页
*/
CREATE PROCEDURE Proc_Paging
(
@TBName NVARCHAR(255) ,
@SQL nVARCHAR(4000), --不带排序语句的SQL语句
@Page int, --页码
@RecsPerPage int, --每页容纳的记录数
@ID VARCHAR(255), --需要排序的不重复的ID号
@Sort VARCHAR(255) , --排序字段及规则
@PageCount INT OUTPUT --总页数
)
AS
BEGIN
DECLARE @sql1 nvarchar(4000)
SET @sql1=N'SELECT @PageCount=COUNT(*)'
+N' FROM '+@tbname
EXEC sp_executesql @sql1, N'@PageCount int OUTPUT',@PageCount OUTPUT
SET @PageCount=(@PageCount+@RecsPerPage-1)/@RecsPerPage
END
BEGIN
DECLARE @Str nVARCHAR(4000)
SET @Str='SELECT TOP '+CAST(@RecsPerPage AS VARCHAR(20))+' * FROM ('+@SQL+') T WHERE T.'+@ID+' NOT IN (SELECT TOP '+CAST((@RecsPerPage*(@Page-1)) AS VARCHAR(20))+' '+@ID+' FROM ('+@SQL+') T9 ORDER BY '+@Sort+') ORDER BY '+@Sort
--PRINT @Str
--EXEC sp_ExecuteSql @Str
--EXEC @Str
DECLARE @Str1 NVARCHAR(400)
DECLARE @Str2 NVARCHAR(400)
SET @Str1 = CAST(@RecsPerPage AS VARCHAR(20))
SET @Str2 = CAST((@RecsPerPage*(@Page-1)) AS VARCHAR(20))
EXEC ( N'SELECT TOP '+@Str1+ N' * FROM ('+@SQL+N') T WHERE T.'+@ID+N' NOT IN (SELECT TOP '+@Str2+N' '+@ID+N' FROM ('+@SQL+N') T9 ORDER BY '+@Sort+N') ORDER BY '+@Sort )
END
GO
存储过程分页
*/
CREATE PROCEDURE Proc_Paging
(
@TBName NVARCHAR(255) ,
@SQL nVARCHAR(4000), --不带排序语句的SQL语句
@Page int, --页码
@RecsPerPage int, --每页容纳的记录数
@ID VARCHAR(255), --需要排序的不重复的ID号
@Sort VARCHAR(255) , --排序字段及规则
@PageCount INT OUTPUT --总页数
)
AS
BEGIN
DECLARE @sql1 nvarchar(4000)
SET @sql1=N'SELECT @PageCount=COUNT(*)'
+N' FROM '+@tbname
EXEC sp_executesql @sql1, N'@PageCount int OUTPUT',@PageCount OUTPUT
SET @PageCount=(@PageCount+@RecsPerPage-1)/@RecsPerPage
END
BEGIN
DECLARE @Str nVARCHAR(4000)
SET @Str='SELECT TOP '+CAST(@RecsPerPage AS VARCHAR(20))+' * FROM ('+@SQL+') T WHERE T.'+@ID+' NOT IN (SELECT TOP '+CAST((@RecsPerPage*(@Page-1)) AS VARCHAR(20))+' '+@ID+' FROM ('+@SQL+') T9 ORDER BY '+@Sort+') ORDER BY '+@Sort
--PRINT @Str
--EXEC sp_ExecuteSql @Str
--EXEC @Str
DECLARE @Str1 NVARCHAR(400)
DECLARE @Str2 NVARCHAR(400)
SET @Str1 = CAST(@RecsPerPage AS VARCHAR(20))
SET @Str2 = CAST((@RecsPerPage*(@Page-1)) AS VARCHAR(20))
EXEC ( N'SELECT TOP '+@Str1+ N' * FROM ('+@SQL+N') T WHERE T.'+@ID+N' NOT IN (SELECT TOP '+@Str2+N' '+@ID+N' FROM ('+@SQL+N') T9 ORDER BY '+@Sort+N') ORDER BY '+@Sort )
END
GO
二
CREATE procedure GetNews
(@pagesize int,
@pageindex int,
@docount bit)
as
set nocount on
if(@docount=1)
select count(id) from news
else
begin
declare @indextable table(id int identity(1,1),nid int)
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
set rowcount @PageUpperBound
insert into @indextable(nid) select id from news order by addtime desc
select O.id,O.source,O.title,O.addtime from news O,@indextable t where O.id=t.nid
and t.id>@PageLowerBound and t.id<=@PageUpperBound order by t.id
end
set nocount off
GO
(@pagesize int,
@pageindex int,
@docount bit)
as
set nocount on
if(@docount=1)
select count(id) from news
else
begin
declare @indextable table(id int identity(1,1),nid int)
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
set rowcount @PageUpperBound
insert into @indextable(nid) select id from news order by addtime desc
select O.id,O.source,O.title,O.addtime from news O,@indextable t where O.id=t.nid
and t.id>@PageLowerBound and t.id<=@PageUpperBound order by t.id
end
set nocount off
GO
三
------------------------------------
--用途:分頁存儲過程(對有主鍵的表效率極高)
--說明:
------------------------------------
CREATE PROCEDURE UP_GetRecordByPage
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 主鍵字段名
@PageSize int = 10, -- 頁尺寸
@PageIndex int = 1, -- 頁碼
@IsReCount bit = 0, -- 返回記錄總數, 非 0 值則返回
@OrderType bit = 0, -- 設置排序類型, 非 0 值則降序
@strWhere varchar(1000) = '' -- 查詢條件 (注意: 不要加 where)
AS
declare @strSQL varchar(6000) -- 主語句
declare @strTmp varchar(100) -- 臨時變量
declare @strOrder varchar(400) -- 排序類型
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName +'] desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
if @PageIndex = 1
begin
set @strTmp =''
if @strWhere != ''
set @strTmp = ' where ' + @strWhere
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + ']' + @strTmp + ' ' + @strOrder
end
if @IsReCount != 0
set @strSQL = 'select count(*) as Total from [' + @tblName + ']'+' where ' + @strWhere
exec (@strSQL)
GO
--用途:分頁存儲過程(對有主鍵的表效率極高)
--說明:
------------------------------------
CREATE PROCEDURE UP_GetRecordByPage
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 主鍵字段名
@PageSize int = 10, -- 頁尺寸
@PageIndex int = 1, -- 頁碼
@IsReCount bit = 0, -- 返回記錄總數, 非 0 值則返回
@OrderType bit = 0, -- 設置排序類型, 非 0 值則降序
@strWhere varchar(1000) = '' -- 查詢條件 (注意: 不要加 where)
AS
declare @strSQL varchar(6000) -- 主語句
declare @strTmp varchar(100) -- 臨時變量
declare @strOrder varchar(400) -- 排序類型
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName +'] desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
if @PageIndex = 1
begin
set @strTmp =''
if @strWhere != ''
set @strTmp = ' where ' + @strWhere
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + ']' + @strTmp + ' ' + @strOrder
end
if @IsReCount != 0
set @strSQL = 'select count(*) as Total from [' + @tblName + ']'+' where ' + @strWhere
exec (@strSQL)
GO
申明
非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!
博文欢迎转载,但请给出原文连接。