Solved

How can i retrive a specific amount of records

Posted on 2004-09-02
9
156 Views
Last Modified: 2008-02-26
I have an App which Querys a database and gets the records from a SQL server

my strSQLlooks like this "SELECT Top 50 CDR.[key],CDR.CallingPartyNumber,CDR.CalledPartyNumber From CDR Order By CDR.[key] DESC"

that gets me the first 50 records but i would like to be able to get the next 50 etc, and go forth and back just like a kind of page control

what is the best way to get this effectively done
0
Comment
Question by:AlexPonnath
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11965259
Try something like this

 "SELECT Top 50 CDR.[key],CDR.CallingPartyNumber,CDR.CalledPartyNumber From CDR WHERE CDR.[key] < LastID Order By CDR.[key] DESC"
0
 
LVL 15

Expert Comment

by:unknown_routine
ID: 11965833
This is a solution:


1: Create a temporary table  based on CDR  name it for example CDRTemp


2:use your code on it

"SELECT Top 50 CDR.[key],CDR.CallingPartyNumber,CDR.CalledPartyNumber From CDR Order By CDR.[key] DESC"


3: Delete those 50 recored from the tempTable :

Do While Not r.EOF

r.Delete
r.MoveNext
Loop


Now again select top 50

SELECT Top 50 CDR.[key],CDR.CallingPartyNumber,CDR.CalledPartyNumber From CDR Order By CDR.[key] DESC"

and continue as above.



any question? please mention

0
 

Author Comment

by:AlexPonnath
ID: 11967514
Thats not realy a valid solution....

a temp table is not good because i could have to potentially move 1 million records if the user does not put a filter in place. Also if i delete them it wont work to page back. Another point is that this data can change on a minute basis...

on the pointer of using a where clause i can see that work but its not very clean since onetime i need to go desc  and then ASC which will have either a bad performance or take a lot of extra index space..

I am also unclear how to retrive the max an min key values for the top 50 query, so that i could use that
for the forward or backward query..
0
Technology Partners: 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!

 
LVL 5

Expert Comment

by:gary_j
ID: 11968397
Hello,

How about this:

"SELECT Top 50 CDR.[key],CDR.CallingPartyNumber,CDR.CalledPartyNumber From CDR Order By CDR.[key] DESC"

then store the last CDR.[key] in a variable by using the .movelast method or do a For Next loop and when you want to get the next 50 do

"SELECT Top 50 CDR.[key],CDR.CallingPartyNumber,CDR.CalledPartyNumber From CDR WHERE CDR.[key] < 'lastKey' Order By CDR.[key] DESC"

For the opposite direction and order by ASC you would again get the last CDR.[key] but do
"SELECT Top 50 CDR.[key],CDR.CallingPartyNumber,CDR.CalledPartyNumber From CDR WHERE CDR.[key] > 'lastKey' Order By CDR.[key] ASC"

Does that help?

0
 
LVL 5

Expert Comment

by:gary_j
ID: 11968754
Sorry, your reply just clicked in my head. You can use the where clause just by reversing the > AND < symbols and the ASC and DESC order though I am not sure about the performance as you say.

As for the Max or Min of the CDR.[key], I think that would have to be done in a separate SQL statement something like this:

"SELECT max(i.[key]) as MaxKey FROM CDR i INNER JOIN CDR a ON i.[key] = a.[key]
where a.[key] IN (SELECT TOP 50(key) FROM CDR order by key ASC)"

MaxKey returns your max then just Min to get the minimum...you get the idea.

Then for going forward or back you could modify (SELECT TOP 50(key) FROM CDR order by key ASC) to look like this (SELECT TOP 50(key) FROM CDR WHERE key > 'lastKey' order by key ASC)" or (SELECT TOP 50(key) FROM CDR WHERE key < 'lastKey' order by key DESC)"

I have tried some tests from Query analyzer and it works there but I am not sure that this is exactly what you are looking for.
0
 
LVL 6

Expert Comment

by:PePi
ID: 11968817

create the stored procedure below:

CREATE PROCEDURE unsp_PAGED_CDR
      (
       @Page int,
       @RecsPerPage int
      )                        
AS

-- We don't want to return the # of rows inserted
-- into our temporary table, so turn NOCOUNT ON
SET NOCOUNT ON


--Create a temporary table
CREATE TABLE #TempCDR
(
      ID int IDENTITY,
      CDRKey int,
      CallingParty nvarchar(50),
      CalledParty nvarchar(50)
)


-- Insert the rows from tblItems into the temp. table
BEGIN
      INSERT INTO #TempCDR
      (
            CDRKey,
            CallingParty,
            CalledParty
      )
      SELECT CDR.[key], CDR.CallingPartyNumber, CDR.CalledPartyNumber
      FROM CDR
      ORDER BY CDR.[key] ASC
END

-- Find out the first and last record we want
DECLARE @FirstRec int, @LastRec int
SELECT @FirstRec = (@Page - 1) * @RecsPerPage
SELECT @LastRec = (@Page * @RecsPerPage + 1)

-- Now, return the set of paged records, plus, an indiciation of we
-- have more records or not!
SELECT *,
       MoreRecords =
      (
       SELECT COUNT(*)
       FROM #TempCDR TI
       WHERE TI.ID >= @LastRec
      )
FROM #TempCDR
WHERE ID > @FirstRec AND ID < @LastRec
ORDER BY ID


-- Turn NOCOUNT back OFF
SET NOCOUNT OFF
GO


to use this stored procedure, pass the page number and the number of records per page. example: if you have 200 records and you set 50 as the number of page, if you pass 2 & 50 as you r first 2 parameters, it will return records 51 to 100. pass 4 & 50 and it will return records 150 to 200. and so on.

the recordset will also return the remaining number of records through "MoreRecords"


0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 11970793
SELECT Top 50 CDR.[key],CDR.CallingPartyNumber,CDR.CalledPartyNumber From CDR where CDR.[key] not in
(select top " & 50*Number & " CDR.[key] from CDR Order By CDR.[key] DESC) Order By CDR.[key] DESC

Number is an integer which specifies the "page" you want to see.If it is 0 you see the first 50.If it is 4 you see from 201 to 250.

That will work if CDR.[key] is a unique primary key
0
 

Author Comment

by:AlexPonnath
ID: 11978478
jyparask

your solution works if there is no additional condition for this query....

how wuld the querylooklike if i would like to only get all recordrs which also meet the folowing condition

where CDR.CalledPartyNumber = '5551212'

0
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
ID: 11987535
SELECT Top 50 CDR.[key],CDR.CallingPartyNumber,CDR.CalledPartyNumber From CDR where CDR.CalledPartyNumber = '5551212' and CDR.[key] not in
(select top " & 50*Number & " CDR.[key] from CDR where CDR.CalledPartyNumber = '5551212' Order By CDR.[key] DESC) Order By CDR.[key] DESC


I've tested it in both MS-SQL and MS-ACCESS
0

Featured Post

Industry Leaders: 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

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

685 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