首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MS Access SQL使用查询(无唯一标识符)或ROW_NUMBER进行分页?

MS Access SQL使用查询(无唯一标识符)或ROW_NUMBER进行分页?
EN

Stack Overflow用户
提问于 2016-03-09 23:29:51
回答 1查看 140关注 0票数 0

我试图一次从SQL查询中抓取100行,并将它们用于分页。该查询从具有100,000+行的表中返回约2000行。但是,该表没有唯一的标识符。

代码语言:javascript
复制
SELECT TOP 100 * FROM tbl_Items WHERE tbl_Items.Repair_Required = true

我已经研究过使用ROW_NUMBER,但似乎在MS Access中不能使用。我还研究了使用Gord Thompson回答的"self-join“创建自定义row_number:Access query producing results like ROW_NUMBER() in T-SQL。然而,将我的100,000+表本身连接起来在性能上并不容易。

我有什么选择?

EN

回答 1

Stack Overflow用户

发布于 2016-03-09 23:41:27

下面是一个使用集合的方法,它的运行速度非常快:

代码语言:javascript
复制
Public Function RowCounter( _
  ByVal strKey As String, _
  ByVal booReset As Boolean, _
  Optional ByVal strGroupKey As String) _
  As Long

' Builds consecutive RowIDs in select, append or create query
' with the possibility of automatic reset.
' Optionally a grouping key can be passed to reset the row count
' for every group key.
'
' Usage (typical select query):
'   SELECT RowCounter(CStr([ID]),False) AS RowID, *
'   FROM tblSomeTable
'   WHERE (RowCounter(CStr([ID]),False) <> RowCounter("",True));
'
' The Where statement resets the counter when the query is run
' and is needed for browsing a select query.
'
' Usage (typical append query, manual reset):
' 1. Reset counter manually:
'   Call RowCounter(vbNullString, False)
' 2. Run query:
'   INSERT INTO tblTemp ( RowID )
'   SELECT RowCounter(CStr([ID]),False) AS RowID, *
'   FROM tblSomeTable;
'
' Usage (typical append query, automatic reset):
'   INSERT INTO tblTemp ( RowID )
'   SELECT RowCounter(CStr([ID]),False) AS RowID, *
'   FROM tblSomeTable
'   WHERE (RowCounter("",True)=0);
'
' 2002-04-13. Cactus Data ApS. CPH
' 2002-09-09. Str() sometimes fails. Replaced with CStr().
' 2005-10-21. Str(col.Count + 1) reduced to col.Count + 1.
' 2008-02-27. Optional group parameter added.

  Static col      As New Collection
  Static strGroup As String

  On Error GoTo Err_RowCounter

  If booReset = True Or strGroup <> strGroupKey Then
    Set col = Nothing
    strGroup = strGroupKey
  Else
    col.Add col.Count + 1, strKey
  End If

  RowCounter = col(strKey)

Exit_RowCounter:
  Exit Function

Err_RowCounter:
  Select Case Err
    Case 457
      ' Key is present.
      Resume Next
    Case Else
      ' Some other error.
      Resume Exit_RowCounter
  End Select
End Function

请研究内联注释和示例。

当然,您可以将它应用于包含2000条记录的查询,而不是源表。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35895840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档