三种SQL分页查询的存储过程代码

复制代码 代码如下:

--根据MAX(MIN)ID

CREATE PROC [dbo].[proc_select_id]

@pageindex int=1,--当前页数

@pagesize int=10,--每页大小

@tablename VARCHAR(50)='',--表名

@fields VARCHAR(1000)='',--查询的字段集合

@keyid VARCHAR(50)='',--主键

@condition NVARCHAR(1000)='',--查询条件

@orderstr VARCHAR(500),--排序条件

@totalRecord BIGINT OUTPUT--总记录数

AS

IF ISNULL(@orderstr,N'')=N'' SET @orderstr=N' ORDER BY '+@keyid+N' DESC '

IF ISNULL(@fields,N'')=N'' SET @fields=N'*'

IF ISNULL(@condition,N'')=N'' SET @condition=N'1=1'

DECLARE @sql NVARCHAR(4000)

--IF(@totalRecord IS NULL)

--BEGIN

SET @sql=N'SELECT @totalRecord=COUNT(*)'

+N' FROM '+@tablename

+N' WHERE '+@condition

EXEC sp_executesql @sql,N'@totalRecord INT OUTPUT',@totalRecord OUTPUT

--END

IF(@pageindex=1)

BEGIN

SET @sql=N'SELECT TOP '+STR(@pagesize)+N' '+@fields+N' FROM '+@tablename+N' WHERE '+@condition+N' '+@orderstr

EXEC(@sql)

END

ELSE

BEGIN

DECLARE @operatestr CHAR(3),@comparestr CHAR(1)

SET @operatestr='MAX'

SET @comparestr='>'

IF(@orderstr<>'')

BEGIN

IF(CHARINDEX('desc',LOWER(@orderstr))<>0)

BEGIN

SET @operatestr='MIN'

SET @comparestr='<'

END

END

SET @sql=N'SELECT top '+STR(@pagesize)+N' '+@fields+N' FROM '+@tablename+N' WHERE '+@keyid+@comparestr

+N'(SELECT '+@operatestr+N'('+@keyid+N') FROM '+@tablename+N' WHERE '+@keyid

+N' IN (SELECT TOP '+STR((@pageindex-1)*@pagesize)+N' '+@keyid+N' FROM '+@tablename+N' WHERE '

+@condition+N' '+@orderstr+N')) AND '+@condition+N' '+@orderstr

EXEC(@sql)

END

GO

--根据ROW_NUMBER() OVER

CREATE PROC [dbo].[proc_select_page_row]

@pageindex INT=1,--当前页数

@pagesize INT=10,--每页大小

@tablename VARCHAR(50)='',--表名

@fields VARCHAR(1000)='*',--查询的字段集合

@keyid VARCHAR(50)='',--主键

@condition NVARCHAR(1000)='',--查询条件

@orderstr VARCHAR(500),--排序条件

@totalRecord BIGINT OUTPUT--总记录数

AS

IF ISNULL(@orderstr,N'')=N'' SET @orderstr=N' ORDER BY '+@keyid+N' DESC '

IF ISNULL(@fields,N'')=N'' SET @fields=N'*'

IF ISNULL(@condition,N'')=N'' SET @condition=N'1=1'

DECLARE @sql NVARCHAR(4000)

-- IF @totalRecord IS NULL

-- BEGIN

SET @sql=N'SELECT @totalRecord=COUNT(*)'

+N' FROM '+@tablename

+N' WHERE '+@condition

EXEC sp_executesql @sql,N'@totalRecord bigint OUTPUT',@totalRecord OUTPUT

--END

IF(@pageindex=1)

BEGIN

SET @sql=N'SELECT TOP '+STR(@pagesize)+N' '+@fields+N' FROM '+@tablename+N' WHERE '+@condition+N' '+@orderstr

EXEC(@sql)

END

ELSE

BEGIN

DECLARE @StartRecord INT

SET @StartRecord = (@pageindex-1)*@pagesize + 1

SET @sql=N'SELECT * FROM (SELECT ROW_NUMBER() OVER ('+ @orderstr +N') AS rowId,'+@fields+N' FROM '+ @tablename+N') AS T WHERE rowId>='+STR(@StartRecord)+N' and rowId<='+STR(@StartRecord + @pagesize - 1)

EXEC(@sql)

END

GO

--根据TOP ID

CREATE PROC [dbo].[proc_select_page_top]

@pageindex INT=1,--当前页数

@pagesize INT=10,--每页大小

@tablename VARCHAR(50)='',--表名

@fields VARCHAR(1000)='',--查询的字段集合

@keyid VARCHAR(50)='',--主键

@condition NVARCHAR(1000)='',--查询条件

@orderstr VARCHAR(500),--排序条件

@totalRecord BIGINT OUTPUT--总记录数

AS

IF ISNULL(@orderstr,N'')=N'' SET @orderstr=N' ORDER BY '+@keyid+N' DESC '

IF ISNULL(@fields,N'')=N'' SET @fields=N'*'

IF ISNULL(@condition,N'')=N'' SET @condition=N'1=1'

DECLARE @sql NVARCHAR(4000)

--IF(@totalRecord IS NULL)

--BEGIN

SET @sql=N'SELECT @totalRecord=COUNT(*)'

+N' FROM '+@tablename

+N' WHERE '+@condition

EXEC sp_executesql @sql,N'@totalRecord INT OUTPUT',@totalRecord OUTPUT

--END

IF(@pageindex=1)

BEGIN

SET @sql=N'SELECT TOP '+STR(@pagesize)+N' '+@fields+N' FROM '+@tablename+N' WHERE '+@condition+N' '+@orderstr

EXEC(@sql)

END

ELSE

BEGIN

SET @sql=N'SELECT TOP '+STR(@pagesize)+N' '+@fields+N' FROM '+@tablename+N' WHERE '+@keyid

+N' NOT IN(SELECT TOP '+STR((@pageindex-1)*@pagesize)+N' '+@keyid+N' FROM '

+@tablename+N' WHERE '+@condition+N' '+@orderstr+N') AND '+@condition+N' '+@orderstr

EXEC(@sql)

END

GO

时间: 2024-10-23 20:03:19

三种SQL分页查询的存储过程代码的相关文章

Oracle、MySQL和SqlServe三种数据库分页查询语句的区别介绍

先来定义分页语句将要用到的几个参数: int currentPage ; //当前页 int pageRecord ; //每页显示记录数 以之前的ADDRESSBOOK数据表为例(每页显示10条记录): 一.SqlServe下载 分页语句 String sql = "select top "+pageRecord +" * from addressbook where id not in (select top "+(currentPage-)*pageRecor

二种sql分页查询语句分享_MsSql

根据题意理解: 本质就是写分页查询: 每页条数:10条: 当前页码:4页: 复制代码 代码如下: //第一种:select * from (select ROW_NUMBER() over(order by Id asc) as num,* from UserInfo)as u where u.num between 10*(4-1)+1 and 10*4//第二种:select top 10 * from UserInfo where Id not in(select top (10*3) i

二种sql分页查询语句分享

根据题意理解: 本质就是写分页查询: 每页条数:10条: 当前页码:4页: 复制代码 代码如下://第一种:select * from (select ROW_NUMBER() over(order by Id asc) as num,* from UserInfo)as u where u.num between 10*(4-1)+1 and 10*4//第二种:select top 10 * from UserInfo where Id not in(select top (10*3) id

三种SQL分页法效率分析

表中主键必须为标识列,[ID] int IDENTITY (1,1) 1.分页方案一:(利用Not In和SELECT TOP分页) 语句形式: SELECT TOP 10 * FROM TestTable WHERE (ID NOT IN (SELECT TOP 20 id FROM TestTable ORDER BY id)) ORDER BY ID SELECT TOP 页大小 * FROM TestTable WHERE (ID NOT IN (SELECT TOP 页大小*页数 id

三种SQL分页方式

1.分页方案一:(利用Not In和SELECT TOP分页) 语句形式: SELECTTOP10*FROMTestTableWHERE(IDNOTIN (SELECTTOP20id FROMTestTable ORDERBYid))ORDERBYIDSELECTTOP页大小*FROMTestTableWHERE(IDNOTIN (SELECTTOP页大小*页数id FROM表 ORDERBYid))ORDERBYID 2.分页方案二:(利用ID大于多少和SELECT TOP分页) 语句形式:

sql分页查询几种写法_MsSql

关于SQL语句分页,网上也有很多,我贴一部分过来,并且总结自己已知的分页到下面,方便日后查阅 1.创建测试环境,(插入100万条数据大概耗时5分钟). create database DBTest use DBTest --创建测试表 create table pagetest ( id int identity(1,1) not null, col01 int null, col02 nvarchar(50) null, col03 datetime null ) --1万记录集 declar

sql分页查询几种写法

关于SQL语句分页,网上也有很多,我贴一部分过来,并且总结自己已知的分页到下面,方便日后查阅 1.创建测试环境,(插入100万条数据大概耗时5分钟). create database DBTest use DBTest --创建测试表 create table pagetest ( id int identity(1,1) not null, col01 int null, col02 nvarchar(50) null, col03 datetime null ) --1万记录集 declar

SQL分页查询方式汇总_MsSql

需求:查询表dbo.Message,每页10条,查询第2页 1:TOP() SELECT TOP(20) * FROM dbo.Message WHERE Code NOT IN (SELECT TOP(10) Code FROM dbo.Message) 2:BETWEEN * AND * , Row_Number() OVER(ORDER BY *) AS rowNum SELECT *,ROW_NUMBER() OVER(ORDER BY Code) AS rowNum INTO #a F

SQL分页查询方式汇总

需求:查询表dbo.Message,每页10条,查询第2页 1:TOP() SELECT TOP(20) * FROM dbo.Message WHERE Code NOT IN (SELECT TOP(10) Code FROM dbo.Message) 2:BETWEEN * AND * , Row_Number() OVER(ORDER BY *) AS rowNum SELECT *,ROW_NUMBER() OVER(ORDER BY Code) AS rowNum INTO #a F