Solved

Divide a recordset

Posted on 2002-06-11
19
348 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:dbnewbie
  • 9
  • 5
  • 3
  • +1
19 Comments
 
LVL 5

Expert Comment

by:spcmnspff
ID: 7071820
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 . . .

=)
0
 
LVL 5

Expert Comment

by:spcmnspff
ID: 7071843
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
0
 

Author Comment

by:dbnewbie
ID: 7072345
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?
0
 
LVL 5

Expert Comment

by:spcmnspff
ID: 7072584
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?
0
 

Author Comment

by:dbnewbie
ID: 7072629
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?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7072941
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 (http://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
0
 
LVL 5

Expert Comment

by:spcmnspff
ID: 7072984
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"
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7073004
>> 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
0
 
LVL 5

Expert Comment

by:spcmnspff
ID: 7073021
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 . . . =)


0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7073044
>>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
0
 
LVL 5

Expert Comment

by:spcmnspff
ID: 7073058
Yes: Access 97 office pack 2, would not let me create a query with a vitual table . . . let me try again . . .
0
 
LVL 5

Expert Comment

by:spcmnspff
ID: 7073064
Yeah, "Syntax error in from clause"  . . . Maybe you have the  MS Jet Update installed . . . I didn't bother with it . . rarely use Access anymore.  =)
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7073081
Access 2000.  Same here, I rarely use it.

Anthony
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7073083
Though it is good to know that syntax does not work with older versions of Access.

Anthony
0
 
LVL 5

Expert Comment

by:spcmnspff
ID: 7073112
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
0
 
LVL 32

Accepted Solution

by:
bhess1 earned 50 total points
ID: 7073236
Using ASP, hmmm... are you using ADO as the connection method?  If so, what you should use are some ADO Recordset properties:  Pagesize, PageCount, and AbsolutePage

Set Pagesize to the number of records you need per page:

rs.Pagesize = 10

Use pagecount to determine the number of pages in the recordset:

NumPages = rs.PageCount

Use AbsolutePage to go to the appropriate page in the recordset.  For example, to display records 31-40 (page 3):

rs.PageSize = 10
rs.AbsolutePage = 3

For iLoop = 1 to rs.PageSize
   <Display Records here>
   rs.Movenext
Next iLoop

This will display records 31 to 40
0
 
LVL 5

Expert Comment

by:spcmnspff
ID: 7073305
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 . . .
0
 

Author Comment

by:dbnewbie
ID: 7074154
bhess, i will try Pagesize, PageCount, and AbsolutePage.
0
 
LVL 32

Expert Comment

by:bhess1
ID: 7074178
It sure simplified our coding when doing paging on our internal website!  We were *really* glad that we found it.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now