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

SQL - 分页存储过程

时间:2016-03-27 11:09:46      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

http://www.jb51.net/article/71193.htm

http://www.webdiyer.com/utils/spgenerator/

create PROCEDURE [dbo].[Proc_SqlPageByRownumber]
(
 @tbName VARCHAR(255),   --表名
 @tbGetFields VARCHAR(1000)= *,--返回字段
 @OrderfldName VARCHAR(255),  --排序的字段名
 @PageSize INT=20,    --页尺寸
 @PageIndex INT=1,    --页码
 @OrderType bit = 0,    --0升序,非0降序
 @strWhere VARCHAR(1000)=‘‘,  --查询条件
 @TotalCount INT OUTPUT   --返回总记录数
)
AS
-- =============================================
-- Author:  allen (liyuxin)
-- Create date: 2012-03-30
-- Description: 分页存储过程(支持多表连接查询)
-- Modify [1]: 2012-03-30
-- =============================================
BEGIN
 DECLARE @strSql VARCHAR(5000) --主语句
 DECLARE @strSqlCount NVARCHAR(500)--查询记录总数主语句
 DECLARE @strOrder VARCHAR(300) -- 排序类型

 --------------总记录数---------------
 IF ISNULL(@strWhere,‘‘) <>‘‘
   SET @strSqlCount=Select @TotalCout=count(*) from  + @tbName +  where 1=1 + @strWhere
 ELSE SET @strSqlCount=Select @TotalCout=count(*) from  + @tbName

 exec sp_executesql @strSqlCount,N@TotalCout int output,@TotalCount output
 --------------分页------------
 IF @PageIndex <= 0 SET @PageIndex = 1

 IF(@OrderType<>0) SET @strOrder= ORDER BY +@OrderfldName+ DESC 
 ELSE SET @strOrder= ORDER BY +@OrderfldName+ ASC 

 SET @strSql=SELECT * FROM 
 (SELECT ROW_NUMBER() OVER(+@strOrder+) RowNo,+ @tbGetFields+ FROM  + @tbName +  WHERE 1=1  + @strWhere+ ) tb 
 WHERE tb.RowNo BETWEEN +str((@PageIndex-1)*@PageSize+1)+ AND  +str(@PageIndex*@PageSize)

 exec(@strSql)
 SELECT @TotalCount
END




DECLARE    @return_value int,
        @TotalCount int

EXEC    @return_value = [dbo].[Sp_LeePageProc]
        @tbName = NT002_StoreInfo,
        @tbGetFields = N*,
        @OrderfldName = N代码,
        @PageSize = 20,
        @PageIndex = 1,
        @OrderType = 0,
        @TotalCount = @TotalCount OUTPUT

 

SQL - 分页存储过程

标签:

原文地址:http://www.cnblogs.com/CyLee/p/5325038.html

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