首页 > 代码库 > 存储过程实现分页
存储过程实现分页
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
存储过程实现分页