存储过程分页

ylbtech-DatabaseDesgin:ylbtech-Proc-存储过程分页

 

1.A,数据库关系图(Database Diagram) 返回顶部
1.B,数据库设计脚本(Database Design Script)返回顶部
USE [test]
GO

/****** Object:  StoredProcedure [dbo].[procCommonPaging]    Script Date: 03/22/2012 13:34:59 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

/* 功能:带分页的存储过程,只支持单表
 * 姓名:
 * 日期:
*/
CREATE PROCEDURE [dbo].[procCommonPaging]
 @pagesize int, --显示的大小
 @pageindex int,--传入的页码
 @keyFiled  varchar(50),--主键字段
 @tablename varchar(150),--表名
 @condition  varchar(500),--查询条件
 @sort varchar(150),--排序
 @rowcount int output,--查询的总记录数
 @pagecount int output --获得总页码
AS   
  -- 查询的总记录数
     declare @sql nvarchar(4000)--要执行的SQl
  declare @expellent int--要排除的记录数
 begin 
  --获取总记录数
  set @sql = 'select @rowcount = count(*) from ' + @tablename+' '+@condition   
  EXEC sp_executesql @sql,N'@rowcount int output',@rowcount output--计算总记录数
  set @pagecount=CEILING((@rowcount*1.0)/@PageSize)--计算总页数 
  if @pageindex=1 
   begin
   set @sql = 'select top '+str(@pagesize)+' * from ' + @tablename+' '+@condition+' '+@sort
   end
   begin
   if @condition=' '
    begin
    set @expellent=@pagesize*(@pageIndex-1) --计算要排除的记录数
    set @sql = 'select top '+str(@pagesize)+' * from ' + @tablename+' where '+@keyFiled+' not in(select top '+STR(@expellent)+' '+@keyFiled+' from '+@tablename+' '+@sort+ ') '+@sort
    end
   else
    begin
    set @expellent=@pagesize*(@pageIndex-1)--计算要排除的记录数
    set @sql = 'select top '+str(@pagesize)+' * from ' + @tablename+' '+@condition+' and '+@keyFiled+' not in(select top '+STR(@expellent)+' '+@keyFiled+' from '+@tablename+' '+@condition+' '+@sort+') '+@sort
    end
      end
      exec(@sql)

 end

GO
View Code
1.C,功能实现代码(Function Implementation Code)返回顶部
warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/ylbtech/p/4126671.html