首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • SQL分页问题 [已结帖,结帖人:sren2008]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • sren2008
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 结帖率:
    发表于:2008-08-22 09:51:22 楼主
    我想 取得 表中 name = 'test' 用户的 从第2条 到 第10条 数据,请问 , SQL 语句应该怎么写?
    40  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • wufeng4552
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-22 09:54:081楼 得分:0
    1.
    select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入
    set rowcount n
    select * from 表变量 order by columnname desc


    2.
    select top n * from
    (select top m * from tablename order by columnname) a
    order by columnname desc


    3.
    如果tablename里没有其他identity列,那么:
    select identity(int) id0,* into #temp from tablename

    取n到m条的语句为:
    select * from #temp where id0 >=n and id0 <= m

    如果你在执行select identity(int) id0,* into #temp from tablename这条语句的时候报错,那是因为你的DB中间的select into/bulkcopy属性没有打开要先执行:
    exec sp_dboption 你的DB名字,'select into/bulkcopy',true


    4.
    如果表里有identity属性,那么简单:
    select * from tablename where identitycol between n and m

    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zjcxc
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 2

      2

    发表于:2008-08-22 09:56:022楼 得分:0
    SQL code
    -- 2005 及之后的版本 SELECT * FROM( SELECT rowid = ROW_NUMBER(ORDER BY GETDATE()), * FROM 你的表 WHERE name = 'test') )A WHERE rowid BETWEEN 2 AND 10
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • sren2008
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-22 10:55:123楼 得分:0
    不好使啊,二楼的兄弟, 他说 关键字 'ORDER' 附近有语法错误。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zoujp_xyz
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-22 11:07:274楼 得分:40
    SQL code
    select top 8 * from (select top 10 * from tb where name='test')b order by id desc
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zjcxc
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 2

      2

    发表于:2008-08-22 11:11:085楼 得分:0
    少打了一点东西
    SQL code
    -- 2005 及之后的版本 SELECT * FROM( SELECT rowid = ROW_NUMBER()OVER(ORDER BY GETDATE()), * FROM 你的表 WHERE name = 'test' )A WHERE rowid BETWEEN 2 AND 10
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zoujp_xyz
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-22 11:11:426楼 得分:0
    给你一个简单的分页例子
    SQL code
    CREATE PROCEDURE [GetCustomersDataPage1] @PageIndex INT, --页面索引,从datagrid中获取 @PageSize INT, --页面显示数量,从datagrid中获取 @RecordCount INT OUT, --返回记录总数 @PageCount INT OUT, --返回分页后页数 @strGetFields nvarchar(1000), -- 需要查询的列 @tableName nvarchar(500) , --表名称 @ID nvarchar(100), --主键,(为表的主键) @strWhere nvarchar(1000) ='', -- 查询条件(注意: 不要加where) @sortName nvarchar(50) =' asc ' , --排序方式 @orderName nvarchar(100) --父级查询排序方式 AS declare @countSelect nvarchar(2000) --设置统计查询语句 if len(@strWhere) =0 --如果没有查询条件 begin set @countSelect=N'SELECT @CountRecord = COUNT(*) FROM '+@tableName end else --否则 begin set @countSelect=N'SELECT @CountRecord = COUNT(*) FROM '+@tableName+' where '+@strWhere end --执行并返回总数 exec sp_executesql @countSelect,N'@CountRecord int output',@RecordCount output SET @PageCount = CEILING(@RecordCount * 1.0 / @PageSize) SET NOCOUNT ON DECLARE @SQLSTR NVARCHAR(3000) --实际总共的页码小于当前页码或者最大页码 if @PageCount>=0 --如果分页后页数大于 begin if @PageCount<=@PageIndex and @PageCount>0 --如果实际总共的页数小于datagrid索引的页数 --or @PageCount=1 begin --设置为最后一页 set @PageIndex=@PageCount-1 end else if @PageCount<=@PageIndex and @PageCount=0 begin set @PageIndex=0; end end IF @PageIndex = 0 OR @PageCount <= 1 --如果为第一页 begin if len(@strWhere) =0 begin SET @SQLSTR =N'SELECT TOP '+STR( @PageSize )+@strGetFields+' FROM '+@tableName+' ORDER BY '+@orderName+@sortName end else begin SET @SQLSTR =N'SELECT TOP '+STR( @PageSize )+@strGetFields+' FROM '+@tableName+' where '+@strWhere+' ORDER BY '+@orderName+@sortName end end ELSE IF @PageIndex = @PageCount - 1 --如果为最后一页 begin if len(@strWhere) =0 begin --SET @SQLSTR =N' SELECT '+@strGetFields+' FROM '+@tableName+' where '+@ID+' not in ( SELECT TOP '+STR(/*@RecordCount - */@PageSize * @PageIndex )+@ID+' FROM '+@tableName+' ORDER BY '+@orderName+@sortName+' ) ORDER BY '+@orderName+@sortName SET @SQLSTR =N' SELECT '+@strGetFields+' FROM '+@tableName+' where '+@ID+' < ( SELECT min('+@ID+') from ( select top '+STR(@PageSize * @PageIndex )+@ID+' FROM '+@tableName+' ORDER BY '+@orderName+@sortName+' ) as i ) ORDER BY '+@orderName+@sortName end else begin --SET @SQLSTR =N' SELECT '+@strGetFields+' FROM '+@tableName+' where '+@ID+' not in ( SELECT TOP '+STR(/*@RecordCount - */ @PageSize * @PageIndex )+@ID+' FROM '+@tableName+' where '+@strWhere+' ORDER BY '+@orderName+@sortName+' ) and '+@strWhere+' ORDER BY '+@orderName+@sortName SET @SQLSTR =N' SELECT '+@strGetFields+' FROM '+@tableName+' where '+@strWhere+ ' and '+@ID+' < ( SELECT min('+@ID+') from ( select top '+STR(@PageSize * @PageIndex )+@ID+' FROM '+@tableName+ ' where '+@strWhere+' ORDER BY '+@orderName+@sortName+' ) as i ) ORDER BY '+@orderName+@sortName end end ELSE --否则执行 begin if len(@strWhere) =0 begin --SET @SQLSTR =N' SELECT TOP '+STR( @PageSize )+@strGetFields+' FROM '+@tableName+' where '+@ID+' not in ( SELECT TOP '+STR( /*@RecordCount - */@PageSize * @PageIndex )+@ID+' FROM '+@tableName+' ORDER BY '+@orderName+@sortName+' ) ORDER BY '+@orderName+@sortName SET @SQLSTR =N' SELECT Top '+STR( @PageSize )+@strGetFields+' FROM '+@tableName+' where '+@ID+' < ( SELECT min('+@ID+') from ( select top '+STR(@PageSize * @PageIndex )+@ID+' FROM '+@tableName+' ORDER BY '+@orderName+@sortName+' ) as i ) ORDER BY '+@orderName+@sortName end else begin --SET @SQLSTR =N' SELECT TOP '+STR( @PageSize )+@strGetFields+' FROM '+@tableName+' where '+@ID+' not in (SELECT TOP '+STR(/*@RecordCount - */ @PageSize * @PageIndex )+@ID+' FROM '+@tableName+' where '+@strWhere+' ORDER BY '+@orderName+@sortName+' )and '+@strWhere+'ORDER BY '+@orderName+@sortName SET @SQLSTR =N' SELECT TOP '+STR( @PageSize )+@strGetFields+' FROM '+@tableName+' where '+@strWhere+ ' and '+@ID+' < ( SELECT min('+@ID+') from ( select top '+STR(@PageSize * @PageIndex )+@ID+' FROM '+@tableName+ ' where '+@strWhere+' ORDER BY '+@orderName+@sortName+' ) as i ) ORDER BY '+@orderName+@sortName end end EXEC (@SQLSTR) set nocount off GO declare @RC nvarchar(20) declare @PC nvarchar(20) exec GetCustomersDataPage1 15,10,@RC output,@PC output,'id,aa,bb,cc,'tb',id,'Area=''广东'' ', ' desc ' , id go
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • WANGXUELLD
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-22 11:18:227楼 得分:0
    jl
    修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    北京创新乐知广告有限公司 版权所有 京 ICP 证 070598 号
    世纪乐知(北京)网络技术有限公司 提供技术支持
    Copyright © 2000-2008, CSDN.NET, All Rights Reserved