Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win



Posted on 2006-06-26
Medium Priority
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 300 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

636 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