创建存储过程的代码

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


-- =============================================
-- Author:        bin
-- Create date: 2009-07-22 12:41
-- Description:    分页,用到了ROW_NUMBER()
-- =============================================
create PROCEDURE [dbo].[proc_SplitPage_wb]
@tblName   varchar(255),       -- 表名
@strFields varchar(1000) = '*', -- 需要返回的列,默认*
@strOrder varchar(255)='',      -- 排序的字段名,必填
@strOrderType varchar(10)='ASC', -- 排序的方式,默认ASC
@PageSize   int = 10,          -- 页尺寸,默认10
@PageIndex int = 1,           -- 页码,默认1
@strWhere varchar(1500) = '' -- 查询条件 (注意: 不要加 where)
AS

declare @strSQL   varchar(5000)

if @strWhere !=''
set @strWhere=' where '+@strWhere

set @strSQL=
'SELECT * FROM ('+
    'SELECT ROW_NUMBER() OVER (ORDER BY '+@strOrder+' '+@strOrderType+') AS pos,'+@strFields+' '+
    'FROM '+@tblName+' '+@strWhere+
') AS sp WHERE pos BETWEEN '+str((@PageIndex-1)*@PageSize+1)+' AND '+str(@PageIndex*@PageSize)


exec (@strSQL)

 

posted @ 2013-02-21 22:24  李只蚊  阅读(312)  评论(0编辑  收藏  举报