分页-sql语句
java 2007-09-26 10:42:21 阅读17 评论0 字号:大中小 订阅
Mysql存储过程:DELIMITER $$;
DROP PROCEDURE IF EXISTS `mytest`.`MyPage`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `MyPage`(
tableName varchar(100), /*表名*/
fieldsName VARCHAR(100), /*显示字段*/
pageIndex int, /*页索引*/
pageSize int, /*每页大小*/
sortName VARCHAR(500), /*排序字段*/
strWhere varchar(500) /*条件*/
)
BEGIN
DECLARE fieldlist VARCHAR(200);
if fieldsName=''||fieldsName=null THEN
set fieldlist='*';
else
set fieldlist=fieldsName;
end if;
if strWhere=''||strWhere=null then
if sortName=''||sortName=null then
set @strSQL=concat('select ',fieldlist,' from ' , tableName,' limit ',(pageindex-1)*pageSize,',',pageSize);
else
set @strSQL=concat('select ',fieldlist,' from ' , tableName,' order by ',sortName,' limit ',(pageindex-1)*pageSize,',',pageSize);
end if;
else
if sortName=''||sortName=null then
set @strSQL=concat('select ',fieldlist,' from ' , tableName,' where ',strWhere,' limit ',(pageindex-1)*pageSize,',',pageSize);
else
set @strSQL=concat('select ',fieldlist,' from ' , tableName,' where ',strWhere,' order by ',sortName,' limit ',(pageindex-1)*pageSize,',',pageSize);
end if;
end if;
PREPARE stmt1 FROM @strSQL;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END$$
DELIMITER ;$$
Mysql: SELECT [FILED] FROM [TABLE] WHERE [CONDITION] LIMIT [START POS],[PAGESIZE];
Oracle:
1.with partdata as (select rownum rowno,t.* from table1 t where t.id>200407160000)
select * from partdata where rowno between 10 and 20
2.select * from (
select id,msisdn,filename, rownum as num
from(select * from tb_quit_msisdn order by id desc)
where rownum<=22
)
where num>=20
3.
SELECT *
FROM (SELECT ROWNUM r,t1.* From 表名称 t1 where rownum < M + N) t2
where t2.r >= M
例如从表Sys_option(主键为sys_id)中从10条记录还是检索20条记录,语句如下:
SELECT *
FROM (SELECT ROWNUM R,t1.* From Sys_option where rownum < 30 ) t2
Where t2.R >= 10
sqlserver:
1. SELECT TOP 页大小 *
FROM TestTable
WHERE (ID NOT IN
(SELECT TOP 页大小*页数 id
FROM 表
ORDER BY id))
ORDER BY ID
2. SELECT TOP 页大小 *
FROM TestTable
WHERE (ID >
(SELECT MAX(id)
FROM (SELECT TOP 页大小*页数 id
FROM 表
ORDER BY id) AS T))
ORDER BY ID
3.
从数据库表中的第M条记录开始取N条记录,利用Top关键字:注意如果Select语句中既有top,又有order by,则是从排序好的结果集中选择:
SELECT *
FROM ( SELECT Top N *
FROM (SELECT Top (M + N - 1) * FROM 表名称 Order by 主键 desc) t1 ) t2
Order by 主键 asc
例如从表Sys_option(主键为sys_id)中从10条记录还是检索20条记录,语句如下:
SELECT *
FROM ( SELECT TOP 20 *
FROM (SELECT TOP 29 * FROM Sys_option order by sys_id desc) t1) t2
Order by sys_id asc
4.
CREATE PROCEDURE PrSqlPager
(
@TableName nvarchar(100), -- 表名
@StrWhere nvarchar(500), -- Where 子句
@IdentityKey nvarchar(100), -- 标识列
@PageNum int, -- 页号
@PageSize int, -- 页大小
@ErrInfo varchar(100) OUTPUT, -- 出错信息
@TotalRecordCount int OUTPUT, -- 总记录数
@PageCount int OUTPUT -- 总页数
)
AS
DECLARE @sql NVARCHAR(1000)
DECLARE @paramDefine NVARCHAR(1000)
/******************** 数据表中总记录数 ***********************/
SET @sql = 'SELECT @TotalRecordCount = COUNT(*) FROM ' + @TableName + ' WHERE 1=1'
IF @StrWhere IS NOT NULL AND @StrWhere <> ''
SET @sql = @sql + ' AND ' + @StrWhere
SET @paramDefine = '@TotalRecordCount INT OUTPUT'
EXEC SP_EXECUTESQL @sql,@paramDefine,@TotalRecordCount OUTPUT
/*************************************************************/
/*********************** 验证页大小 **************************/
IF @PageSize < 0
SET @PageSize = 0
IF @PageSize > @TotalRecordCount
SET @PageNum = @TotalRecordCount
/*************************************************************/
/******************** 根据页大小计算总页数 ***********************/
IF @TotalRecordCount % @PageSize = 0
SET @PageCount = @TotalRecordCount / @PageSize
ELSE
SET @PageCount = @TotalRecordCount / @PageSize + 1
/*************************************************************/
/*********************** 验证页号 ***************************/
IF @PageNum < 1
SET @PageNum = 1
IF @PageNum > @PageCount
SET @PageNum = @PageCount
/*************************************************************/
/******************** 实现分页查询 ***********************/
DECLARE @foreRecordCount INT
SET @foreRecordCount = (@PageNum - 1) * @PageSize + 1
SET @sql = 'SELECT TOP ' + CAST(@PageSize as varchar)
+ ' * FROM ' + @TableName
+ ' WHERE ' + @IdentityKey + ' >= '
+ ' ( SELECT MAX( ' + @IdentityKey + ' ) FROM ( SELECT TOP '
+ CAST(@foreRecordCount as varchar)
+ @IdentityKey + ' FROM ' + @TableName
+ ' ORDER BY ' + @IdentityKey + ' ) AS TempTable ) '
+ ' ORDER BY ' + @IdentityKey
IF @StrWhere IS NOT NULL AND @StrWhere <> ''
SET @sql = @sql + ' AND ' + @StrWhere
SET @paramDefine = N'@PageSize INT,@foreRecordCount INT'
EXEC SP_EXECUTESQL @sql,@paramDefine,@PageSize,@foreRecordCount
/*************************************************************/
GO