Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How To Get Last Inserted Record Value In SQL Server


How To Get Last Inserted Record Value In SQL Server

Author
Message
sqlvogel
sqlvogel
SSC-Addicted
SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)

Group: General Forum Members
Points: 484 Visits: 3706
TheSQLGuru (4/27/2010)
Tables are UNORDERED sets of data unless they have a CLUSTERED INDEX on them.


Tables are unordered sets even if they do have a clustered index. A clustered index is just one form of internal storage. It doesn't impose any logical ordering any more than a nonclustered index on a heap does.
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44994 Visits: 39880
David Portas (4/27/2010)
TheSQLGuru (4/27/2010)
Tables are UNORDERED sets of data unless they have a CLUSTERED INDEX on them.


Tables are unordered sets even if they do have a clustered index. A clustered index is just one form of internal storage. It doesn't impose any logical ordering any more than a nonclustered index on a heap does.


Heh... obviously not a fan of WITH (INDEX(0)) or the "Quirky" Update, huh?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
sqlvogel
sqlvogel
SSC-Addicted
SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)

Group: General Forum Members
Points: 484 Visits: 3706
Jeff Moden (4/27/2010)
Heh... obviously not a fan of WITH (INDEX(0)) or the "Quirky" Update, huh?


WITH (INDEX(0)) doesn't mean a table is ordered. It just specifies a hint for an execution plan. In fact it doesn't even order the query results AFAIK because the INDEX hint without ORDER BY results in an unordered scan. Rows might be returned in allocation order rather than cluster key order or they might be reordered by a merry-go-round scan.
vidya_pande
vidya_pande
SSC Eights!
SSC Eights! (891 reputation)SSC Eights! (891 reputation)SSC Eights! (891 reputation)SSC Eights! (891 reputation)SSC Eights! (891 reputation)SSC Eights! (891 reputation)SSC Eights! (891 reputation)SSC Eights! (891 reputation)

Group: General Forum Members
Points: 891 Visits: 242
The order in which records are inserted does not hold any significance unless you specifically track the insert time details.
Also the order in which records are stored does not hold any significance in the table if you do not have clustered index or auto increment or ordered data.



Scott Coleman
Scott Coleman
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2942 Visits: 1415
Just to make sure we completely cover the topic:

An IDENTITY column will help you find the most recently-inserted row, unless IDENTITY_INSERT or DBCC CHECKIDENT (RESEED) are used to insert records out of order.

A TIMESTAMP column will allow you to find the row used in the most recent INSERT or UPDATE.

A DATETIME column with DEFAULT GETDATE() will work for single-row inserts, but if multiple rows are inserted in the same statement they will all have the same value.



sqlvogel
sqlvogel
SSC-Addicted
SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)

Group: General Forum Members
Points: 484 Visits: 3706
Scott Coleman (4/28/2010)
An IDENTITY column will help you find the most recently-inserted row


Possibly it will if your inserts are serialized. If you have multiple connections with multi-statement transactions then it all depends how you define "most recent". It is quite possible to have identity values interleaved from two near-simultaneous operations for example even though one of them could commit before the other. I think it is safest to assume IDENTITY order is non-deterministic and will not necessarily match insertion order (whatever "insertion order" means) except in the special case of a single connection inserting one row at a time.
TheSQLGuru
TheSQLGuru
SSCertifiable
SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)

Group: General Forum Members
Points: 5941 Visits: 8299
>>A DATETIME column with DEFAULT GETDATE() will work for single-row inserts, but if multiple rows are inserted in the same statement they will all have the same value.

Here is another incorrect statement too (actually more than one failure).

1) That DEFAULT only helps if the INSERT doesn't explicitly state a value for the DATETIME column, which is certainly possible.

2) You are at the mercy of the 3.33ms precision of the DATETIME datatype. A busy system could easily have more than one concurrent insert within that interval - both will receive the same datetime value.

Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
Eric M Russell
Eric M Russell
SSCarpal Tunnel
SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)

Group: General Forum Members
Points: 4580 Visits: 9502
The technical puzzle proposed in this discussion is interesting, but it has obviously run it's course. I'm interested about why you would need to know the "last" value inserted into a table that contains only a single column called [Name]. For example, are you trying to page through the list of names in an application, or perhaps you're incrementally loading data and need to know where you left off? If you describe more detail about your intended goal, then perhaps someone can offer a better solution to the actual problem.


"The universe is complicated and for the most part beyond your control, but your life is only as complicated as you choose it to be."
JasonClark
JasonClark
Old Hand
Old Hand (394 reputation)Old Hand (394 reputation)Old Hand (394 reputation)Old Hand (394 reputation)Old Hand (394 reputation)Old Hand (394 reputation)Old Hand (394 reputation)Old Hand (394 reputation)

Group: General Forum Members
Points: 394 Visits: 793
we can use SELECT @@IDENTITY, SELECT SCOPE_IDENTITY(), SELECT IDENT_CURRENT(‘TableName’) to get last inserted record: https://mostafaelmasry.com/2016/05/09/how-to-find-the-last-inserted-record-in-sql-server/
drew.allen
drew.allen
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2599 Visits: 9878
JasonClark (7/22/2016)
we can use SELECT @@IDENTITY, SELECT SCOPE_IDENTITY(), SELECT IDENT_CURRENT(‘TableName’) to get last inserted record: https://mostafaelmasry.com/2016/05/09/how-to-find-the-last-inserted-record-in-sql-server/

Since all of these methods depend on the table having an identity column, and the OP stated that no such column exists, none of these will work.

Also, you want to be careful about resurrecting old threads, unless you have something that will add to the discussion. The OP for this thread has not logged on since 2010, so is extremely unlikely to return to answer any questions.

Drew

J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search