Solved

How can i retrive a specific amount of records

Posted on 2004-09-02
9
155 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

861 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