SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Retrieve the last record of a table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

spider3
Starting Member

11 Posts

Posted - 07/10/2007 :  04:34:46  Show Profile  Visit spider3's Homepage  Reply with Quote
Hi!
I needto find the last record that has been inserted on a table.
If I perform a SELCT * FROM mytable , will I receive the data based on the inserted order (natural order)? In that case i'll simply need to check the last record of the results I got in my application to find the last inserted one?
(no I am not looking for an identity number ^^)

thank you

harsh_athalye
Flowing Fount of Yak Knowledge

India
5532 Posts

Posted - 07/10/2007 :  04:39:00  Show Profile  Visit harsh_athalye's Homepage  Click to see harsh_athalye's MSN Messenger address  Send harsh_athalye a Yahoo! Message  Reply with Quote
You can't rely on the order in which the SELECT statement fetch records unless you specify ORDER BY. To determine First or Last record, you have specify column which can be used to specify order (e.g. inserted_date column).

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22755 Posts

Posted - 07/10/2007 :  06:37:43  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
If you use identity column or datetime as default value,then find maximum on that

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Kristen
Test

United Kingdom
22403 Posts

Posted - 07/10/2007 :  06:41:47  Show Profile  Reply with Quote
"I needto find the last record that has been inserted on a table"

SELECT TOP 1 * FROM mytable ORDER BY SomeColumn DESCENDING

[SomeColumn] needs to reflect the insertion order - e.g. IDENTITY or Date/Time

Relational Databases don't have any inherent order to the data - if another query has just run, selecting part of the data, that will influence the order that SQL Server "sees" the data in the next query.

So you need a column that defines the "insertion order"

"(no I am not looking for an identity number ^^)"

I reckon you probably are

Kristen
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 07/11/2007 :  02:22:24  Show Profile  Reply with Quote
There should be a standard sticky topic for this question!
And another one for "how do I get my rows into columns".
Go to Top of Page

spider3
Starting Member

11 Posts

Posted - 07/12/2007 :  04:52:15  Show Profile  Visit spider3's Homepage  Reply with Quote
thank you for your suggestions - the previous system was based on dBase and there was a kind of natural order that was used to retrieve this value. Of course having an identity date/time insertion is more simple ^_^
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22755 Posts

Posted - 07/12/2007 :  05:02:08  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Is there a meaning for ^_^?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000