473,398 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Select specific number of records

Dear Group

I wonder whether you can give me a syntax example for a SQL Statement.

Lets assume I've a table containing three columns ContactID (Primary Key),
Firstname and Lastname.

I would like to write a stored procedure which returns me the first ten
records and increments an outside variable each time it runs.
E.g If I run it the first time I pass the variable as 0 and it will return
me the first ten records and returns the variable value 1.
When run a second time, I will pass the variable as 1 and it will return me
records 11-20 and sets the variable to 2 and so on...

The difficult thing is how to tell to return me records 11-20. I can't use
the ContactID as someone might have deleted a row and e.g. ContactID 18 is
missing. In this case I only would get 9 rows returned. It always should be
ten.

Thanks very much for your time and efforts!

Kind Regards,

Martin

"There are 10 types of people in this world: Those that understand binary
arithmetic, and those that don't."
Jul 20 '05 #1
3 8692
This is called paging; see some possible techniques here.

http://www.aspfaq.com/2120

--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/


"Martin Feuersteiner" <th************@hotmail.com> wrote in message
news:bu**********@sparta.btinternet.com...
Dear Group

I wonder whether you can give me a syntax example for a SQL Statement.

Lets assume I've a table containing three columns ContactID (Primary Key),
Firstname and Lastname.

I would like to write a stored procedure which returns me the first ten
records and increments an outside variable each time it runs.
E.g If I run it the first time I pass the variable as 0 and it will return
me the first ten records and returns the variable value 1.
When run a second time, I will pass the variable as 1 and it will return me records 11-20 and sets the variable to 2 and so on...

The difficult thing is how to tell to return me records 11-20. I can't use
the ContactID as someone might have deleted a row and e.g. ContactID 18 is
missing. In this case I only would get 9 rows returned. It always should be ten.

Thanks very much for your time and efforts!

Kind Regards,

Martin

"There are 10 types of people in this world: Those that understand binary
arithmetic, and those that don't."

Jul 20 '05 #2
See:

http://www.sqlserverfaq.com/content/...wer.aspx?ID=51

And while you are at that site, you can search for more articles about
"paging". Tony Rogerson, who runs the site, has done quite a bit in that
area.

--
Jacco Schalkwijk
SQL Server MVP
"Martin Feuersteiner" <th************@hotmail.com> wrote in message
news:bu**********@sparta.btinternet.com...
Dear Group

I wonder whether you can give me a syntax example for a SQL Statement.

Lets assume I've a table containing three columns ContactID (Primary Key),
Firstname and Lastname.

I would like to write a stored procedure which returns me the first ten
records and increments an outside variable each time it runs.
E.g If I run it the first time I pass the variable as 0 and it will return
me the first ten records and returns the variable value 1.
When run a second time, I will pass the variable as 1 and it will return me records 11-20 and sets the variable to 2 and so on...

The difficult thing is how to tell to return me records 11-20. I can't use
the ContactID as someone might have deleted a row and e.g. ContactID 18 is
missing. In this case I only would get 9 rows returned. It always should be ten.

Thanks very much for your time and efforts!

Kind Regards,

Martin

"There are 10 types of people in this world: Those that understand binary
arithmetic, and those that don't."

Jul 20 '05 #3
"Martin Feuersteiner" <th************@hotmail.com> wrote in message
news:bu**********@sparta.btinternet.com...
Dear Group

I wonder whether you can give me a syntax example for a SQL Statement.

Lets assume I've a table containing three columns ContactID (Primary Key),
Firstname and Lastname.

I would like to write a stored procedure which returns me the first ten
records and increments an outside variable each time it runs.
E.g If I run it the first time I pass the variable as 0 and it will return
me the first ten records and returns the variable value 1.
When run a second time, I will pass the variable as 1 and it will return me
records 11-20 and sets the variable to 2 and so on...

The difficult thing is how to tell to return me records 11-20. I can't use
the ContactID as someone might have deleted a row and e.g. ContactID 18 is
missing. In this case I only would get 9 rows returned. It always should be
ten.

Thanks very much for your time and efforts!

Kind Regards,

Martin

"There are 10 types of people in this world: Those that understand binary
arithmetic, and those that don't."


Certainly check out the links Aaron and Jacco referred to for a comprehensive
treatment of the topic.

CREATE TABLE Contacts
(
contact_id INT NOT NULL PRIMARY KEY,
last_name VARCHAR(20) NOT NULL,
first_name VARCHAR(20) NOT NULL
)

-- The first page is 0
CREATE PROCEDURE NextContacts
@contact_page_number INT OUTPUT,
@number_of_contacts INT = 10
AS
SELECT C.contact_id, C.last_name, C.first_name
FROM (SELECT C1.contact_id, COUNT(*) AS rank
FROM Contacts AS C1
INNER JOIN
Contacts AS C2
ON C2.contact_id <= C1.contact_id
GROUP BY C1.contact_id) AS CC
INNER JOIN
Contacts AS C
ON CC.rank > @contact_page_number * @number_of_contacts AND
CC.rank <= (@contact_page_number + 1) * @number_of_contacts AND
C.contact_id = CC.contact_id
ORDER BY CC.rank ASC
SET @contact_page_number = @contact_page_number + 1

Regards,
jag
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tom Urbanowicz | last post by:
I have a table with 100+ columns, for which I'm trying to retrieve only 1 specific record. For this single record, I do not know which of the columns are NULL, and which are populated. I would...
6
by: RC | last post by:
My code below will loop through all the records in the table, and when the if statement is true it goes to the ***Me.ContainerNumberProductsTable = GETContainerNumber.Value*** bit like should but...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
1
by: plaforest | last post by:
Hello All, Thank you for your thoughtful consideration. I am running Access 2000 (9.0.3821 SR-1) This query works: SELECT , FROM table1
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which...
6
markrawlingson
by: markrawlingson | last post by:
Hopefully someone can help me out with this, it's driving me nuts... I have two tables - We'll call them table1 and table2. Table1 holds event information, and table2 holds user registration data...
2
by: tamaker | last post by:
Is this do-able with ASP / VBscript? -- I have a database with user records (name, photo, etc. etc.) I want to use asp to generate (on the homepage) a series of 4 randomly selected 'user...
5
by: Chris Cowles | last post by:
I use an application that uses Oracle 8.1.7. All functions of the application are completed with calls to stored procedures. A data entry error occurred that caused thousands of records to be...
5
by: Ronald S. Cook | last post by:
From my business tier (class) I get back an IQueryable<Penof data. Here is my client code that works fine: PenClass penClass = new PenClass(); IQueryable<Penpens = penClass.SelectPens(); ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.