Wrox Programmer Forums
|
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 3rd, 2011, 11:39 AM
Authorized User
 
Join Date: Dec 2009
Posts: 85
Thanks: 16
Thanked 0 Times in 0 Posts
Default getting latest record in db dependent on date

Hi there

I'm inserting a record into a sql db and then immediately needing that record back - in the table I have an id field which is auto incrementing and I also have a date field (datetime).

What's the best way to do this?

I'm using "select top 1 * FROM mytable" but I actually want the latest dated one.

So I would need something like:

"select * from mytable WHERE dateadded = LatestDate" though am not sure of the syntax for 'LatestDate' if you see what I mean.

thanks

Adam
 
Old January 3rd, 2011, 11:43 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

What you *could* do is order by date descending and get the first record:

SELECT TOP 1 * FROM mytable ORDER BY dateadded DESC

However, you probably don't want this. If two users insert a record at the same time, you run the risk of getting the wrong record back.

Take a look at the following article to see some ways to get back the record's iderntity:

http://www.adopenstatic.com/experime...autonumber.asp

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old January 3rd, 2011, 05:14 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

In other words, just use SELECT @@IDENTITY

But one caution: Be sure you use the *SAME* connection (and do NOT close and reopen it!) for the INSERT and then the SELECT @@IDENTITY
 
Old January 4th, 2011, 09:56 AM
Authorized User
 
Join Date: Dec 2009
Posts: 85
Thanks: 16
Thanked 0 Times in 0 Posts
Default getting last record in db

Thanks guys - I got there with your help.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting distinct and latest record elygp SQL Language 4 May 4th, 2008 10:49 PM
get latest record frm transaction tbl jyopallavi SQL Server 2000 1 June 9th, 2005 08:11 AM
Retreive latest and greatest record alexvgs Oracle 3 April 25th, 2005 09:57 PM
SQL Query - picking latest record and group by markw SQL Language 2 April 6th, 2005 03:54 AM
3 Drop Down box w/ db values & dependent choices smarks Classic ASP Databases 1 March 2nd, 2005 03:32 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.