Link to home
Start Free TrialLog in
Avatar of dbnewbie
dbnewbie

asked on

Divide a recordset

Say I have a 100 records of whatever. I want to run a SQL statement against it but somehow break up the recordset in groups of, say, 10. Meaning to say, the first set would have records 1-10, the next set 11-20, and so on until 91-100.

I kind of want to mimic what EE does with its questions -- under "Questions waiting to be answered," it only lists the latest 20 unanswered question. You can click the link that says "Next 50" to see the rest of the unanswered questions.
Avatar of spcmnspff
spcmnspff
Flag of United States of America image

There must be an Identity field on the table.  The Create this proc:

    Create Proc spFetchChunk
    @From Int,
    @To Int
    AS

    Declare @SQL VarChar(200)

    Set @SQL = 'select TOP ' + Convert(Varchar,@To-@From) + ' *
    from
        (select TOP ' + Convert(Varchar,@From) + ' *
        from Table
        order by ID ASC) AS AA
    order by ID DESC'

    --Print @SQL
    Exec (@SQL)
    Go

(Be sure to replace the word table with table name you're actually dealing with) Now to test run this:

exec spFetchChunk 5, 10

You should be abble to call this proc from your front end with something likr ADO passing the start and stop record number as parameters . . .

=)
Sorry that first proc doesn;t work . . . use this one instead:

    Create Proc spFetchChunk
    @From Int,
    @To Int
    AS

    Declare @SQL VarChar(200), @Rows Int

    Set @SQL = 'Select * From (select TOP ' + Convert(Varchar,(@To - @From + 1)) + ' *
    from
        (select TOP ' + Convert(Varchar,@To) + ' *
        from TitleAuthor
        order by ID ASC) AS AA
    order by ID DESC) B
    Order By ID ASC'

--    Print @SQL
    Exec (@SQL)
    Go

Have fun =D
Avatar of dbnewbie
dbnewbie

ASKER

oh oh.

i'm not using MS-SQL. i'm actually developing this in ASP. just needed some help w the SQL. will this work?
No this is strictly an MS-SQL solution,  If you have a writing asp with a MSSQL backend, this will work.  Which DBMS are you using?
isn't SQL supposed to be a standard? anyway, all i'm connecting to is an MS Access database. however, should i choose to upgrade later, i would like my SQL code to still work. is it possible?
Avatar of Anthony Perkins
Sure SQL is a "standard", that does not mean that does not prevent every product under the sun, even from the same company, producing it's own extensions, so no, in general they are not totally compatible.  Since this is a MS-SQL topic area the solutions offered will be those that work in a MS SQL Server environment and for Access type questions you would be better off in the MS Access topic area (https://www.experts-exchange.com/msaccess/)

But in any case what you are looking for is something in the lines of the following.  Supposing you had a recordset with 50 rows (using the Customers table in the Northwind database) then the following would give you the first 10 rows:

Select Top 10 * From Customers Order By CustomerID

For the next 10 rows (11 to 20)
Select Top 10 * From  (Select Top 40 * from Customers Order by Customerid Desc) Order By CustomerID

For the next 10 rows (21 to 30)
Select Top 10 * From  (Select Top 30 * from Customers Order by Customerid Desc) Order By CustomerID

For the next 10 rows (31 to 40)
Select Top 10 * From  (Select Top 20 * from Customers Order by Customerid Desc) Order By CustomerID

For the last 10 rows:
Select Top 10 * From  (Select Top 10 * from Customers Order by Customerid Desc) Order By CustomerID

Anthony
There are SQL standards, but they aren't always strictly adhered to.
The problem is that the TOP functionality is strictly a T-SQL feature.

Okay this will work in Access with a little modification:
Use this to concatenate your sql.  Then use this as the sql for an ADO or DAO recordset.

 Dim SQL, A, B
 A= 5
 B = 10
 
 SQL ="Select * from Table Where JobID In" & _
    "(Select Top " & B-A+1 &" JobId From Table where JobId In" & _
        "(Select TOP " & B & " JobID from Table order by JobID ASC)" & _
    "Order By JobId DESC)" & _
"Order by JobID ASC"
>> The problem is that the TOP functionality is strictly a T-SQL feature.<<
Actually the TOP keyword has been in MS Access a lot longer than MS SQL Server.  It was only introduced to MS SQL Server in version 7.

Anthony
Yes, Anthony, but I consider Jet SQL a crippled T-SQL syntax anyway.  With differences like using * instead of % and # to designate dates, etc.  Jet SQL wants to be T-SQL but just doesn't deliver all the functionality.

BTW: Your's doesn't work with Access because JET SQL doesn't support virtual tables. Use a subquery instead . . . =)


>>BTW: Your's doesn't work with Access because JET SQL doesn't support virtual tables. Use a subquery
instead . . . =)<<
That is odd, because I tested it before posting.  Did you?

Anthony
Yes: Access 97 office pack 2, would not let me create a query with a vitual table . . . let me try again . . .
Yeah, "Syntax error in from clause"  . . . Maybe you have the  MS Jet Update installed . . . I didn't bother with it . . rarely use Access anymore.  =)
Access 2000.  Same here, I rarely use it.

Anthony
Though it is good to know that syntax does not work with older versions of Access.

Anthony
Yeah, access is a good tool to get something done quickly,  but it can be quite abusive to your SQL server . . . just don't link any SQL tables . . . =D
ASKER CERTIFIED SOLUTION
Avatar of Brendt Hess
Brendt Hess
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's cool Bhess1! . . . It pays to your ADO =)

Makes sense though. If you consider that an ADO recordset is really just a cursor anyway, then there should be a builtin paging fucntionality . . .
bhess, i will try Pagesize, PageCount, and AbsolutePage.
It sure simplified our coding when doing paging on our internal website!  We were *really* glad that we found it.