Solved

How can i retrive a specific amount of records

Posted on 2004-09-02
9
152 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now