Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 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 June 5th, 2007, 06:53 AM
Registered User
 
Join Date: Jul 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to dayaananthanm
Default How to get last inserted record in a table??

How to get last inserted record in a table... In my table only primary key sequence is there..no timestamp and no identity columns... How can we achive this same task if we don't have the primary key sequence also....

M.Dayananthan - Make New Ideas That Make Your Work Interesting!!
 
Old June 5th, 2007, 07:16 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Dunno if you can change the table but why not just add an IDENITY column? Saves a load of headaches...

--Jeff Moden
 
Old June 6th, 2007, 05:52 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Though a costly solution, you may want to write a trigger on the table in question for INSERT and store the newly inserted key column's value in a separate table. And you may use that as reference to get the last inserted row.

But an Identity column will save all that, as Jeff said.

_________________________
- Vijay G
Strive for Perfection
 
Old June 6th, 2007, 07:10 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Even then, what's the trigger going to do if you have something that inserts more than 1 row in the table? If it's an exclusive "GUI" related table where you will never have more than 1 row inserted for any given INSERT, then a trigger will absolutely do the trick... but like Vijay and I have both said now, adding an IDENTITY column will save all that.

--Jeff Moden
 
Old June 6th, 2007, 11:22 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Yes, Trigger is not the apt solution to this (for batch/bulk inserts). When done in a batch, still OP could track only the last row inserted in the batch (not for each row inserted), when he says...
Quote:
quote:How to get last inserted record in a table
_________________________
- Vijay G
Strive for Perfection
 
Old June 7th, 2007, 07:15 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Understood... but how?... What determines the "last" row inserted in a batch without either an IDENTITY or a TimeStamp?

--Jeff Moden
 
Old June 11th, 2007, 09:15 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Not being a smart guy here... would really like to know how to do this without an IDNETITY or a TimeStamp...

--Jeff Moden





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Edit record right after it was Inserted? aladov ASP.NET 2.0 Professional 4 December 11th, 2007 03:18 PM
Record is Inserted Twice donevco Access 1 February 27th, 2007 12:28 PM
Return to Inserted Record - FormView rit01 ASP.NET 2.0 Basics 15 January 27th, 2007 12:04 PM
Last record inserted ID ADAC Programming VB Databases Basics 1 June 5th, 2006 02:41 PM
Getting just one record from Table knowledge76 Access VBA 8 May 23rd, 2005 07:55 AM





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