Home > Ask the Oracle Database / Applications 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?


>

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
How to check SQL query construction with the Mimer Validator
Using the SQL GROUP BY clause for counting combinations
How to use an SQL CASE expression
How to sort an SQL UNION query with special ORDER BY sequence
How to use string functions to make an SQL join
An SQL solution for a customer order homework problem
How to use SQL's POSITION function with substrings
Using SQL date functions to get totals for last three days
Using CASE in the SQL ORDER BY clause
What's the difference between an SQL inner join and equijoin?

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



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

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




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