Posted on 2006-06-26
Last Modified: 2013-12-09
I am using FIRST and SKIP (Firebird SQL) for recordset paging to reduce the time it takes for my records to travel from the server to the client.

This works great because instead of sending thousands of records across the wire to be paged by my ASP code, only the 10, 20, 50 etc. records that are selected will be sent.

In order to calculate how many records to skip, I need to get a count of how many records are in the recordset. That means I am running a SQL statement to get the recordcount and then another SQL statement to actually get the records. There must be a way to combine these statements? Currently, I am only getting the recordcount on the first run but this won't account for newly updated transactions.

'Records per page
      recordsPerPage = Request("recordsPerPage")
      If recordsPerPage = "" Then recordsPerPage = 10

'Current page
      currentPage = Request("currentPage")
      If currentPage = "" Then currentPage = 1

' Create and Open Database/Recordset Object
      Call dbConn(objConn)
      Call openRS(objRS)
      objRS.CursorLocation = 3
      objRS.CacheSize = recordsPerPage
' How many records in recordset? This is what I would like to replace/combine with following SQL statement
' Only runs on the first time through but doesn't account for new transactions
      If Request("howManyRecords") = "" Then
            objRS.Open "Select COUNT(AGPDR) AS howManyRecords From TABLEA", objConn
            howManyRecords = objRS("howManyRecords")
            howManyRecords = Request("howManyRecords")
      End If
'Calculate records to skip
      totalPages = (howManyRecords/recordsPerPage)
      temp = fix(totalPages)
      if totalPages-temp <> 0 then totalPages = temp+1 else totalPages=temp
      numSkip =  (currentPage * recordsPerPage) - recordsPerPage
      If CINT(numSkip) > CINT(howManyRecords) then numSkip = howManyRecords - recordsPerPage      

' Open database and retrieve records
      strSQL = "Select FIRST " &CINT(recordsPerPage)& " SKIP " &CINT(numSkip)& " FIELD1, FIELD2, FIELD3 "
      strSQL = strSQL & "FROM TABLEA"
      objRS.Open strSQL, objConn

response.write "Number of records: " &howManyRecords& "<br>"
response.write "Total Pages: " &totalPages& "<br>"
response.write "Current Page: " &currentPage& "<br>"
response.write "Skip: " &numSkip& "<br><br>"


<table border="0" cellspacing="0" cellpadding="2">
 <tr><td colspan="3"></td></tr>


Do While Not objRS.EOF
 <tr><td colspan="3"></td></tr>


Call closeRS(objRS)
Call closeDB(objConn)  



<!-- Begin Page Navigation -->
<form name="MovePage" method="POST" action="thisPage.asp">
      <input type="hidden" name="howManyRecords" value="<%=howManyRecords%>">
      <input type="hidden" name="totalPages" value="<%=totalPages%>">

      <!-- The dropdown box for selecting the number of records to display -->
      <select name="recordsPerPage" onchange="MovePage.submit()">
            <option value="10">10</option>
              <option value="20">20</option>
                   <option value="50">50</option>
'Display Next / Prev buttons
If currentPage > 1 then
      'Show previous buttons
      Response.Write("<INPUT TYPE=BUTTON VALUE=&lt;&lt; ONCLICK=""document.location.href='thisPage.asp?currentPage=1&recordsPerPage="&recordsPerPage&"&howManyRecords="&howManyRecords&"';""> ")
      Response.Write("<INPUT TYPE=BUTTON VALUE=&lt; ONCLICK=""document.location.href='thisPage.asp?currentPage="&currentPage-1&"&recordsPerPage="&recordsPerPage&"&howManyRecords="&howManyRecords&"';""> ")
End If

If CInt(currentPage) <> CInt(totalPages) then
      'Show next buttons
      Response.Write("<INPUT TYPE=BUTTON VALUE=&gt; ONCLICK=""document.location.href='thisPage.asp?currentPage="&currentPage+1&"&recordsPerPage="&recordsPerPage&"&howManyRecords="&howManyRecords&"';""> ")
      Response.Write("<INPUT TYPE=BUTTON VALUE=&gt;&gt; ONCLICK=""document.location.href='thisPage.asp?currentPage="&totalPages&"&recordsPerPage="&recordsPerPage&"&howManyRecords="&howManyRecords&"';""> ")
End If  
         &nbsp; Go To Page

<select name="currentPage" onchange="MovePage.submit()">
' Dynamically generate page numbers
      For i = 1 to totalPages
            If i = CInt(currentPage) Then y = "selected" else y = ""
            Response.Write "<OPTION " &y& " VALUE="&i&">" & i & "</OPTION>"

Page <%=currentPage%> of <%=totalPages%>
Question by:yourbudweiser
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 19

Accepted Solution

NickUpson earned 100 total points
ID: 16985447
"I am only getting the recordcount on the first run but this won't account for newly updated transactions" - actually you won't see the new updates/insert/deletes until the transaction making those changes does a commit and then your code does a commit. Unless you are connection with autocommit on, which would be unusual.

There is no way to combine the count of rows & the rows themselves unless you use a stored procedure something like this

RETURNS (recs int)

select count(*) from mytable where col1 = :emp_u into :recs;
suspend; /* returns 1 row */

FOR select emp_u from mytable where col1 = :emp_u into :recs
 suspend; /* returns 1 row at a time */

Author Comment

ID: 16992554
is this the only solution you see to calculate how many records to skip? is there a way to put the recordcount into a variable and then use it in the same SQL statement?
LVL 19

Expert Comment

ID: 16993000
in the same sql statement - no, but the approach above will only require one statement to execute it - select * from myproc(34) and could equally well be passed the first and skip parameters as well

Author Comment

ID: 17003488
thanks for the help but I cannot use the SP, it would require modifying my web pages way too much :-)

I will examine and possibly use at another time.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

729 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