sql2005 存储过程实现分页的四个例子

发布时间:2019-07-13编辑:脚本学堂
为大家提供四个sql2005 存储过程分页的例子,有需要的朋友,可以参考下。

分页示例1:
 

复制代码 代码示例:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

/* 创建过程/函数 P_ShowPage。    */
/* 创建过程/函数 P_ShowPage。  */
ALTER PROCEDURE [dbo].[SP_ShowPage]
(
@CurrentPage int =1, /*当前页*/
int = 1, /*从第几条记录开始*/
@PageSize int = 20, /*每页查询的记录条数*/
@Sql varchar(3000), /*查询语句*/
@WhereSQL varchar(3000), /*查询语句中的限定条件,where关键字的部分*/
@OtherSQL varchar(3000), /*查询语句中除select/where外的其他语句*/
@SortColumnID varchar(100), /*表中的主键列名*/
@RecTotal int = 0 Output /*表中的记录总数*/
)

AS

/*自定义的declare变量与输入变量需用以下方法拼接*/
--exec ('select '+@SortColumnID+' from '+ @TableName+' ' + ' '+@OtherSQL)
--print @Sql+' ' + ' '+@OtherSQL
    exec(@Sql+' ' + ' '+@OtherSQL)
set @RecTotal = @@RowCount

declare @frontSQL varchar(3000),@BackSQL varchar(3000),@cp varchar(100)
Set @cp = convert(varchar(100),@PageSize * @CurrentPage)

Set @frontSQL = left(@Sql,6)+' top '+convert(varchar(100),@PageSize)+substring(@Sql,7,len(@Sql)-6)
Set @BackSQL = substring(@Sql,charindex(' from ',@Sql),len(@Sql)-charindex(' from ',@Sql)+6)
--print(@frontSQL+' where ('+@SortColumnID+' not in (select top '+@cp+' '+@SortColumnID+' '+@BackSQL+@OtherSQL+'))'+@OtherSQL)
if ltrim(rtrim(@WhereSQL))=''
  exec(@frontSQL+' where ('+@SortColumnID+' not in (select top '+@cp+' '+@SortColumnID+' '+@BackSQL+' '+@OtherSQL+'))'+@OtherSQL)
else
    begin
    declare @tempWhere varchar(3000)
    set @tempWhere = replace(' '+@WhereSQL,' where ',' and ')

    exec(@frontSQL+' where ('+@SortColumnID+' not in (select top '+@cp+' '+@SortColumnID+' '+@BackSQL+' '+@WhereSQL+' '+@OtherSQL+') '+')'+@tempWhere+@OtherSQL)
    end
RETURN

分页示例2:
 

复制代码 代码示例:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE [dbo].[SP_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(200)        -- 临时变量(查询条件过长时可能会出错,可修改100为1000)
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)

分页示例3:
 

复制代码 代码示例:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE [dbo].[SP_GetRecordByPageOrder]

@tblName varchar(255),   -- 表名
@fldName varchar(255),   -- 显示字段名
@OrderfldName varchar(255), -- 排序字段名
@StatfldName 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(200)   -- 临时变量(查询条件过长时可能会出错,可修改100为1000)
declare @strOrder varchar(400) -- 排序类型
--如果为降序
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @OrderfldName +'] desc'
end
else --否则为升序
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @OrderfldName +'] asc'
end

--获得sql主语句例如 select top str(10) TeacherID from Teacher_Table where
--Birthday<(select min(Birthday) from (select top str((@PageIndex-1)*@PageSize)
--(@PageIndex-1)*@PageSize即已经显示过的数量
-- Birthday from Teacher_Table order by Birthday desc ) as tblTmp) )
set @strSQL = 'select top ' + str(@PageSize) + ' ' + @fldName + ' from ['
+ @tblName + '] where [' + @OrderfldName + ']' + @strTmp + '(['
+ @OrderfldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @OrderfldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder

if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + ' ' + @fldName + ' from ['
+ @tblName + '] where [' + @OrderfldName + ']' + @strTmp + '(['
+ @OrderfldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @OrderfldName + '] 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) + ' ' + @fldName + ' from ['
+ @tblName + ']' + @strTmp + ' ' + @strOrder
end

--如果ISReCount 不等于零返回记录总数
if @IsReCount != 0
set @strSQL = @strSQL+' select count(1) as Total from [' + @tblName + ']'
if @strWhere!=''
set @strSQL = @strSQL+' where ' + @strWhere
exec (@strSQL)

分页示例4:
 

复制代码 代码示例:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

------------------------------------
--用途:执行带分页的存储过程
--项目名称:学生作业管理系统
--说明:根据课程编号和学生的编号获取该课程教师的发布作业情况
------------------------------------
ALTER PROCEDURE [dbo].[SP_IssueHomeWork_Table_GetPageListAndCourse]
@PageIndex int,
@PageSize int,
@StudentID char(10),
@CourseID int
AS
begin    --将当前页加1*每页显示的条目的大小存入临时表中
SELECT top(@PageSize*(@PageIndex+1)) Cast(a.PublishDate AS datetime)as PublishDate, a.WorkID, a.WorkName, a.TeacherID,
                      b.TeacherName, a.CourseID, c.CourseName, a.MaxTime,
                      a.TeacherExplanation, a.IssueWorkPath,IDENTITY(INT,1,1)AS ID --添加ID字段以备后用
into #table FROM         dbo.IssueHomeWork_Table as a INNER JOIN
                      dbo.Teacher_Table as b ON a.TeacherID = b.TeacherID INNER JOIN
                      dbo.Course_Table as c ON a.CourseID = c.CourseID INNER JOIN
                      dbo.TeacherClass_Table as d ON b.TeacherID = d.TeacherID INNER JOIN
                      dbo.Student_Table as e ON d.ClassID =e.ClassID
where and
order by a.PublishDate desc
SELECT TOP(@PageSize)* FROM #table       --获得存储的临时表中的ID>当前页*每页显示的条目

WHERE ID>@PageSize*@PageIndex;
drop table #table
end