码迷,mamicode.com
首页 > 数据库 > 详细

SQL Server 通用分页存储过程

时间:2015-12-01 17:57:17      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

if object_ID([proc_SelectForPager]) is not null
    Drop Procedure [proc_SelectForPager]
Go
Create Proc proc_SelectForPager
(
  @Tb_name varchar(max) ,--表名
  @Order varchar(4000) ,--排序字段
  @CurrentPage int ,--当前页
  @Field varchar(400),--查询字段
  @PageSize int,--每页显示记录数
  @TotalCount int output
)
As

Declare @Exec_sql nvarchar(max)

/* 查询总记录数*/                
Set @Exec_sql=Set @TotalCount=(Select Count(1) From +@Tb_name+ As a)
Exec sp_executesql @Exec_sql,N@TotalCount int output,@TotalCount output
 
 /*设置排序字段*/
Set @Order=isnull( Order by +nullif(@Order,‘‘), Order By getdate())

/*分页查询*/
    Set @Exec_sql=
    ;With CTE As
    (
        Select *,row_number() Over(+@Order+) As r From (Select * from +@Tb_name+) As a 
    )
    Select +@Field+ From CTE Where r Between (@CurrentPage-1)*@pagesize+1 And @CurrentPage*@pagesize Order By r
    
 
 
Exec sp_executesql @Exec_sql,N@CurrentPage int,@PageSize int,@CurrentPage,@PageSize
 
Go

 

SQL Server 通用分页存储过程

标签:

原文地址:http://www.cnblogs.com/langhua/p/5010777.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!