This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes Retrieving data from table in the order of insertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Retrieving data from table in the order of insertion" Watch "Retrieving data from table in the order of insertion" New topic

Retrieving data from table in the order of insertion

Shashank Anand

Joined: Oct 30, 2008
Posts: 26

I have a table with columns custname (VARCHAR(20) NOT NULL), orderno (INT NOT NULL). If I use SELECT * FROM table to retrieve data, the data is displayed in the ascending order of custname and then orderno. But I want the data to be displayed in the order of insertion into the table. Will anybody please tell me which query should I use to do this ?

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32481

The database doesn't promise to store date in insertion order. The only way to obtain this info is to add a column representing the order (via a sequence) when you insert the data.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Paul Campbell
Ranch Hand

Joined: Oct 06, 2007
Posts: 338
Shasshank, is it possible your orderNo column is sequential and the record is inserted at the time the orderNo is assigned to the customer? If so you could do

If not, you have to do what Jeanne suggests (or something similar... like add a column that holds the datetime stamp for the rows creation).
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Retrieving data from table in the order of insertion
jQuery in Action, 3rd edition