Home > Ask the Oracle Experts > SQL Questions & Answers > The "next" composite primary key
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

The "next" composite primary key

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 21 June 2004

Using SQL v 8.00, I would like to write a generic SQL statement that will find the "next record" (based on whatever ORDER BY clause you specify) from the given key when there is a composite primary key.

Example: Here's my table with two columns:

itemId locId
 I01     1
 I01     4
 I01    10
 I2      1
 I2      4

Is it possible to write one generic SQL statement that will return the "next" row (i.e. one row in my result set) when my key is ('I01',4)? It should return ('I01',10). I've tried to concatenate the fields, but have problems with left justified integers. Is this possible without using a cursor?


>
EXPERT RESPONSE

Everything in a relational database is possible without cursors. Thanks for asking.

Okay, some things are more efficient with a cursor, but those are exceedingly rare. <grin />

You said "SQL v 8.00" which presumably is Microsoft SQL Server, so here's a solution that will work for that particular database system:

declare @searchItem varchar(9)
declare @searchLoc integer

set @searchItem ='I01'
set @searchLoc  =4

select top 1
       itemId, locId
  from yourtable
 where itemid > @searchItem
    or (
       itemid = @searchItem
   and locID  > @searchLoc
       )
order
    by itemId, locId

itemId locId

 I01    10   

This isn't strictly "generic" SQL, since it uses the Microsoft-specific TOP keyword.

For More Information


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
SQL
IN list or series of OR conditions?
Connecting tables in a database
SQL query for co-authored books
Querying complex derived tables
SQL string functions
Changing a NULL column to NOT NULL
SQL for hourly totals for the last 48 hours
LEFT OUTER JOIN to a MIN/MAX row
Normalizing a crosstab table
Querying metadata and data at the same time

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts