您的位置:首页 > 其它

存储过程实现分页

2014-12-30 09:48 204 查看
USE [HDIS]

GO

/****** Object: StoredProcedure [dbo].[AspNetPager] Script Date: 12/30/2014 09:00:35 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

Create procedure [dbo].[AspNetPager]

(@tablename nvarchar (1000), --表名

@filedname nvarchar (4000), --查询的字段

@startIndex int, --起始记录数

@endIndex int, --结束记录数

@where nvarchar (4000), --条件 (不包含where)

@orderfiled nvarchar (100), --排序字段 (CreateDate desc)

@PageSize int,

@prmkeyName nvarchar (100),

@pageIndex int,

@docount bit)

as

begin

declare @date varchar(50),@sql nvarchar (4000) ,@i int

select @date =CONVERT(nvarchar(50), serverproperty('productversion'))

--if(CONVERT(int, SUBSTRING(@date,0,3))>8) ------sql2000以上

-- begin

-- if(@docount=1)

-- set @sql = 'select count(*) from ' + @tablename +' where ' + @where

-- else

-- begin

-- set @sql ='

-- with temptbl as (

-- SELECT ROW_NUMBER() OVER (ORDER BY '+ @orderfiled +' )AS Row, * from '+ @tablename +' where '+ @where +')

-- SELECT '+ @filedname +' FROM temptbl where Row between '+CONVERT(nvarchar(100),@startIndex) +' and '+CONVERT(nvarchar(100),@endIndex )

-- END

-- exec (@sql)

-- end

--else

begin -------sql2000

if(@docount=1)

set @sql = 'select count(*) from ' + @tablename +' where ' + @where

else

begin

set @i= CONVERT(nvarchar(100),@PageSize)*(CONVERT(nvarchar(100),@pageIndex)-1)

set @sql = 'SELECT TOP '+ CONVERT(nvarchar(100),@PageSize) +' *

FROM ' + @tablename +' WHERE ('+@where +' and
'+@prmkeyName+' NOT IN

(SELECT TOP '+CONVERT(nvarchar(100),@i)+' '
+@prmkeyName +'

FROM ' + @tablename +' WHERE ' + @where+' ORDER BY '+ @orderfiled +')) ORDER BY '+ @orderfiled

end

--print(@sql)

exec (@sql)

end

end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: