Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 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 September 22nd, 2003, 10:56 AM
Authorized User
 
Join Date: Sep 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to prabodh_mishra
Default Accessing most recent record?

Dear All,

Can anybody tell me that how can I access the last inserted record. Table is indexed.

Hoping for response,
Prabodh

__________________
Prabodh
 
Old September 22nd, 2003, 12:12 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

What do you mean by "last"?

Remember that a table is a set and by definition a set is unordered; yet the concept of "last" implies an ordering of some form or another...

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old September 23rd, 2003, 09:14 AM
Authorized User
 
Join Date: Sep 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to prabodh_mishra
Default

Hi Jeff,
'last' meant with the record inserted most recently from any terminal. Do I need to put any TIMESTAMP against each record or one more table that can keep track of the most recent record. I certainly don't want to do either of these two. Is there any in-built function that could help me?

 
Old September 23rd, 2003, 09:54 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

You've defined 'last' to be the most recent row (not record, BTW) inserted into a table. In order to determine the relative recency of any given row implies that the rows are ordered by some time attribute; that is, there is some value in the row which implicitly or explicitly orders the rows in time. Remember that rows in a table are inherently unordered, so the 'last' row inserted into a table could in fact reside anywhere at all in the table, relative to the other rows. You need something to tell you the relative or absolute time that the row was inserted. The system does not do this for you.

Thus, to find the most recently inserted row means you must define some attribute of that row which orders it in time with respect to the other rows. One way to do this is to define an attribute of the row (i.e. a column) to contain a datetime data type. You can define a DEFAULT constraint on the column of current_timestamp (or GetDate() - they're equivalent). INSERTs will then automatically assign the current time to this column whenever the row is inserted; then finding the row with a maximum value of that column is straightforward.

Another way is to assign a monotonically increasing distinct value to some column every time a row is inserted. You could assign this value by manually incrementing a counter, or by assigning the identity attribute to the column. Again, to find the 'latest' entry is to simply find the maximum value of this column.

I suppose you could also create an auxiliary table which pointed to the 'latest' row, but managing this table seems problematic to me. YMMV.

Note that in SQL Server the TIMESTAMP datatype is a misnomer. In fact, a column of this data type is just a binary value guaranteed to be unique within a database, and is updated whenever a row is updated (and not just inserted). It actually has nothing to do with time. As I understand it, the definition does not guarantee that values will in fact increase, only that they are unique. The lack of this guarantee makes it unsuitable as a temporal ordering column, IMO. OTOH, I have always observed this value to steadily increase, but if you want to depend on this behavior you do so at your own risk.

You say you don't want to add any ordering information to your table, but to do as you wish, you have no choice.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old September 24th, 2003, 09:54 AM
Authorized User
 
Join Date: Sep 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to prabodh_mishra
Default

Thanx for a quick response Jeff






Similar Threads
Thread Thread Starter Forum Replies Last Post
Most Recent Date Ludgero SQL Server ASP 1 August 29th, 2006 12:48 PM
How to get the most recent value from a column method Access 0 July 3rd, 2005 03:32 PM
Most recent list bmains Forum and Wrox.com Feedback 3 June 29th, 2004 07:38 AM
Most recent ID Colonel Angus SQL Server 2000 6 March 31st, 2004 09:54 PM





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