通用存储过程分页

USE [Test]
GO
/****** Object:  StoredProcedure [dbo].[PROC_INFO_LIST_PAGESIZE]    Script Date: 03/31/2011 09:41:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:
-- Create date: 2010.12.17
-- Description: 用于一般用途的存储过程分页
-- =============================================
ALTER PROC [dbo].[PROC_INFO_LIST_PAGESIZE]
@COLUMNNAME VARCHAR(8000), --查询列名称
@ORDERNAME VARCHAR(8000), --排序列名称
@ORDER VARCHAR(10)=NULL, --显示顺序(升序为ASC,降底为DESC,默认为ASC)
@TABLENAME VARCHAR(200), --数据源名称
@PAGEINDEX INT, --结果集页序号
@PAGESIZE INT, --每页容量
@STRWHERE VARCHAR(8000)=NULL    --分页条件
AS
DECLARE @RUNSQL NVARCHAR(MAX)
SET NOCOUNT ON
SET @RUNSQL=N'WITH PAGECONTENT AS'
IF ISNULL(@ORDER,'')=''
BEGIN
SET @RUNSQL=@RUNSQL+N' (SELECT '+@COLUMNNAME+',ROW_NUMBER() OVER(ORDER BY '+@ORDERNAME+' ASC) AS ROWNUMBER FROM '+@TABLENAME
END
ELSE
BEGIN
SET @RUNSQL=@RUNSQL+N' (SELECT '+@COLUMNNAME+',ROW_NUMBER() OVER(ORDER BY '+@ORDERNAME+' '+@ORDER+') AS ROWNUMBER FROM '+@TABLENAME
END
IF(ISNULL(@STRWHERE,'')='')
BEGIN
SET @RUNSQL=@RUNSQL+') '
SET @RUNSQL=@RUNSQL+N'SELECT * FROM PAGECONTENT WHERE ROWNUMBER>'+STR((@PAGEINDEX-1)*@PAGESIZE)+' AND ROWNUMBER<='+STR(@PAGEINDEX*@PAGESIZE)+';'
SET @RUNSQL=@RUNSQL+N'SELECT COUNT(1) AS TOTALNUMBER FROM '+@TABLENAME +' '
print @runsql
END
ELSE
BEGIN
SET @RUNSQL=@RUNSQL+' WHERE '+@STRWHERE+') '
SET @RUNSQL=@RUNSQL+N'SELECT * FROM PAGECONTENT WHERE ROWNUMBER>'+STR((@PAGEINDEX-1)*@PAGESIZE)+' AND ROWNUMBER<='+STR(@PAGEINDEX*@PAGESIZE)+';'
SET @RUNSQL=@RUNSQL+N'SELECT COUNT(1) AS TOTALNUMBER FROM '+@TABLENAME +' WHERE '+@STRWHERE
print @runsql
END
EXEC SP_EXECUTESQL @RUNSQL

转载于:https://www.cnblogs.com/jianjunyue/archive/2011/03/31/2000617.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值