Home > Database > Mysql Tutorial > 存储过程实现分页

存储过程实现分页

WBOY
Release: 2016-06-07 16:12:58
Original
1075 people have browsed it

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

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(nvarcha【本文来自鸿网互联 (http://www.68idc.cn)】r(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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template