In Access97 SQL how do I return a range of rows?

后端 未结 2 1693

I have an Access97 database (unfortunately) and I am querying it over ODBC.

I want to construct a query that will return me J ordered rows starting at offset K. Rig

2条回答
  •  忘掉有多难
    2021-01-06 18:36

    You're basically trying to mimic ROW_NUMBER() in Access. Unfortunately, Access is not a good engine for doing this kind of thing. It gets dog slow after a hundred or so records.

    With that said, here's what you can do:

    SELECT t1.ID, t1.SomeText, t1.SomeDate,
    (
      SELECT COUNT(*) + 1
      FROM Table1 AS t2
      WHERE t1.SomeText > t2.SomeText
    ) as RowNumber
    FROM Table1 t1
    ORDER BY t1.SomeText
    

    That will produce this resultset (in my particular table):

    ID | SomeText             | SomeDate  | RowNumber
    10 | 4NGJN                | 1/20/2010 | 1
    11 | ABH                  | 1/20/2010 | 2
    2  | asldkfj              | 1/20/2010 | 3
    1  | asldkfjaslgh         | 1/20/2010 | 4
    7  | ewoiuhdkjnlbkjbn     | 1/20/2010 | 5
    4  | oihkjldhnlkmdfn,mn   | 1/20/2010 | 6
    3  | oihoih               | 1/20/2010 | 7
    5  | qwwern               | 1/20/2010 | 8
    8  | SKN                  | 1/20/2010 | 9
    9  | WEOIN                | 1/20/2010 | 10
    6  | wetrhn               | 1/20/2010 | 11
    12 | XDNKJ                | 1/20/2010 | 12
    

    I would make a saved query in Access (a view in a SQL Server) and then write a query like this in your application:

    SELECT ID, SomeText, SomeDate, RowNumber
    FROM myView
    WHERE RowNumber > 5
      AND RowNumber < 11
    

    Of course, "5" & "11" you would pass in from your client app, and would be based on the particular page you're on. (It would probably be 50 and 100 or something for you).

提交回复
热议问题