How can i retrive a specific amount of records

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
AlexPonnathAsked:
Who is Participating?
 
Ioannis ParaskevopoulosConnect With a Mentor Commented:
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
 
mladenoviczCommented:
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
 
unknown_routineCommented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
AlexPonnathAuthor Commented:
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
 
gary_jCommented:
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
 
gary_jCommented:
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
 
PePiCommented:

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
 
Ioannis ParaskevopoulosCommented:
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
 
AlexPonnathAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.