473,800 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8710
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.btint ernet.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.btint ernet.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.btint ernet.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_n umber INT OUTPUT,
@number_of_cont acts 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_n umber * @number_of_cont acts AND
CC.rank <= (@contact_page_ number + 1) * @number_of_cont acts AND
C.contact_id = CC.contact_id
ORDER BY CC.rank ASC
SET @contact_page_n umber = @contact_page_n umber + 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
15694
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 like to create a dynamically-generated SELECT--limiting the columns to only those that are populated. If, for example, only columns COL1, COL8, and COL93 are populated for this one record in the MYTEST table, the generated SELECT statement would...
6
4640
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 it does not make the change/update in the table. Any ideas? Dim rst As Object Set rst = Me.Recordset.Clone If Not rst.EOF Then Me.Bookmark = rst.Bookmark
22
12496
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=" & msDbFilename moConn.Properties("Persist Security Info") = False moConn.ConnectionString = msConnString moConn.CursorLocation = adUseClient moConn.Mode = adModeReadWrite' or using default...same result
1
3094
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
4033
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 are not bound, I select from the bottom set and add to the top set which works fine, but now i decide to remove an item from the top set. when i tried to use a remove item code it worked fine, it did delete the item form the list but it added...
6
24766
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 pertaining to each event in table1. So for each record in Table1 there could be hundreds of records pertaining to that record in Table2 - I am trying to count those records (to reveal the number of people registered for the event held in table1) I...
2
2236
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 records' from the database -- say just the headshot photo or name from the database. In addition to the recordset being randomly generated (i.e. our of about 50 records, only records 4, 18, 23 and 26 are displayed) I need
5
2266
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 created with a consistent error in a single field. I can identify those easily records with a select statement. I'd *really* rather not have to change them all manually. I do have access to run a simple update query to correct only the field in...
5
11156
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(); However, I want to then select a subset of this data from within the client. I tried this: IQueryable<PenpensSubset = from p in pens select p.PenId, p.PenNumber;
0
9690
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10275
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10033
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.