dbTalk Databases Forums  

SQL Challenge - how to return a record set starting at a particular record?

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss SQL Challenge - how to return a record set starting at a particular record? in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
michealp@gmail.com
 
Posts: n/a

Default SQL Challenge - how to return a record set starting at a particular record? - 02-24-2007 , 08:08 PM






I have a directory of user information. What I would like to do is
allow someone to search for person X and then return not only the
information for person X, but also the information for the next 15
people following person X sorted alphabetically by lastname.

So if someone searched for the lastname = "Samson", it would return:

Samson, John
Saxton, Greg
Scott, Heather
Sears, Rebecca
.... (15 names following "Samson) ...


How do you in SQL return a record set of X records starting at
particular record (e.g. lastname = "Smith)?

Thanks in advance.


Reply With Quote
  #2  
Old   
Russ Rose
 
Posts: n/a

Default Re: SQL Challenge - how to return a record set starting at a particular record? - 02-24-2007 , 08:57 PM






SELECT TOP 15 firstname, lastname
FROM TableX
WHERE lastname >= 'Samson'
ORDER BY lastname

<michealp (AT) gmail (DOT) com> wrote

Quote:
I have a directory of user information. What I would like to do is
allow someone to search for person X and then return not only the
information for person X, but also the information for the next 15
people following person X sorted alphabetically by lastname.

So if someone searched for the lastname = "Samson", it would return:

Samson, John
Saxton, Greg
Scott, Heather
Sears, Rebecca
... (15 names following "Samson) ...


How do you in SQL return a record set of X records starting at
particular record (e.g. lastname = "Smith)?

Thanks in advance.




Reply With Quote
  #3  
Old   
Ed Murphy
 
Posts: n/a

Default Re: SQL Challenge - how to return a record set starting at a particularrecord? - 02-24-2007 , 09:02 PM



michealp (AT) gmail (DOT) com wrote:

Quote:
I have a directory of user information. What I would like to do is
allow someone to search for person X and then return not only the
information for person X, but also the information for the next 15
people following person X sorted alphabetically by lastname.

So if someone searched for the lastname = "Samson", it would return:

Samson, John
Saxton, Greg
Scott, Heather
Sears, Rebecca
.... (15 names following "Samson) ...


How do you in SQL return a record set of X records starting at
particular record (e.g. lastname = "Smith)?
set rowcount 15

select last_name, first_name
from the_table
where last_name >= 'Smith'
order by last_name

set rowcount 0

(I forget, how does/doesn't SELECT TOP 15 work in SQL 2K vs 2K5? I
haven't had occasion to need to remember.)


Reply With Quote
  #4  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: SQL Challenge - how to return a record set starting at a particular record? - 02-25-2007 , 04:18 AM



Ed Murphy (emurphy42 (AT) socal (DOT) rr.com) writes:
Quote:
(I forget, how does/doesn't SELECT TOP 15 work in SQL 2K vs 2K5? I
haven't had occasion to need to remember.)
They work the same. But SQL 2005 also has SELECT TOP(<expr)), so that
you can have a variable - or something even more complex.



--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #5  
Old   
Greg D. Moore \(Strider\)
 
Posts: n/a

Default Re: SQL Challenge - how to return a record set starting at a particular record? - 02-25-2007 , 10:46 AM



"Erland Sommarskog" <esquel (AT) sommarskog (DOT) se> wrote

Quote:
Ed Murphy (emurphy42 (AT) socal (DOT) rr.com) writes:
(I forget, how does/doesn't SELECT TOP 15 work in SQL 2K vs 2K5? I
haven't had occasion to need to remember.)

They work the same. But SQL 2005 also has SELECT TOP(<expr)), so that
you can have a variable - or something even more complex.

With the caveat of course (since it'll come up I'm sure) that SELECT TOP x
with ORDER BY in a view does NOT work in 2005 (doesn't really work in 2000
except when it does. ;-)





--
Greg Moore
SQL Server DBA Consulting
sql (at) greenms.com http://www.greenms.com




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.