SELECT问题,在线等

hkongm 2003-11-18 02:26:19
在分页结构中,如果表中记录数有许多,怎样控制SELECT语句往RS对象中读10条语句?不是top关键字
PS:用*是肯定没问题的,但是记录数太多就会影响性能,求答案
...全文
41 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
hkongm 2003-11-18
  • 打赏
  • 举报
回复
邹老师看来没理解我的意思,你说的方法我都会啊
那我再问多一点
如果rs对象的pagesize与AbsolutePage两个属性都设置了
再select时,是不是取记录就会跟据这两个属性取了?
比如我要找的记录在101条,pagesize=0,AbsolutePage =10
再显示这条记录内容时是不是select就直接从101开始取的?前面100条就跳过去了吧?
zjcxc 元老 2003-11-18
  • 打赏
  • 举报
回复
--更多的方法参考我的贴子:

查询第X页,每页Y条记录
http://expert.csdn.net/Expert/topic/2365/2365596.xml?temp=.4872705
zjcxc 元老 2003-11-18
  • 打赏
  • 举报
回复

'ASP中的分页例子,以SQL数据库为例,ACCESS数据库只需要更改连接字符串
<%
iConc = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=用户名;Password=密码;Initial Catalog=数据库名;Data Source=SQL服务器名"
Set iRe =Server.CreateObject("ADODB.Recordset")
With iRe
.CursorLocation = adUseClient
.Open "表名", iConc, 1,1
.PageSize = 10 '每页的大小
iCount = .PageCount '总页数
.AbsolutePage = 2 '设置当前显示第几页,这里是第2页
For iJ = 1 To .PageSize '循环显示当前页的记录
'这里改为显示处理的代码
.MoveNext
If .EOF Then Exit For
Next
End With

iRe.Close
Set iRe = Nothing
%>


zjcxc 元老 2003-11-18
  • 打赏
  • 举报
回复
不用top的话,你可以在程序中,用ADO的分页来处理.

下面是VB中的例子:


程序中的话,最好用ADO的分页功能来实现.

'** ----- 数据库连接字符串模板 ---------------------------------------
'** ACCESS数据库
'** iConcStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
'** "Password=""密码"";Data Source=数据库名"
'**
'** SQL数据库
'** iConcStr = "Provider=SQLOLEDB.1;Persist Security Info=True;" & _
'** "User ID=用户名;Password=密码;Initial Catalog=数据库名;Data Source=SQL服务器名"

'VB中的分页例子,以ACCESS数据库为例,SQL数据库只需要更改连接字符串
'引用:Microsoft ActiveX Data Objects 2.x Library
'2.x是版本号
Sub split()
Dim iRe As ADODB.Recordset
Dim iConc As String, iCount&, iI&, iJ&

iConc = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
";Data Source=F:\My Documents\客户资料.mdb"
Set iRe = New ADODB.Recordset
With iRe
.CursorLocation = adUseClient
.Open "客户", iConc, adOpenKeyset, adLockOptimistic
.PageSize = 10
iCount = .PageCount
For iI = 1 To iCount
.AbsolutePage = iI
For iJ = 1 To .PageSize
Debug.Print .Fields(1)
.MoveNext
If .EOF Then Exit For
Next
Next
End With

iRe.Close
Set iRe = Nothing
End Sub



zjcxc 元老 2003-11-18
  • 打赏
  • 举报
回复
最基本的处理方法:

如果表中有主键(记录不重复的字段也可以),可以用类似下面的方法,当然x,y要换成具体的数字,不能用变量:

select top y * from 表 where 主键 not in(select top (x-1)*y 主键 from 表)



如果表中无主键,可以用临时表,加标识字段解决.这里的x,y可以用变量.

select id=identity(int,1,1),* into #tb from 表
select * from #tb where id between (x-1)*y and x*y-1
hkongm 2003-11-18
  • 打赏
  • 举报
回复
后面的where子句能否跟此rs的pagesize属性有所联系呢?
lynx1111 2003-11-18
  • 打赏
  • 举报
回复
取前10条
select top 10 * from tablename order by columnname
lynx1111 2003-11-18
  • 打赏
  • 举报
回复
--------------------四种方法取表里n到m条纪录: -------------------------

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


34,874

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧