Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-Server-2008
Hi.
I would like to retrieve the latest inserted or even updated identity from a table.but whenever i write the following code there is no results at all.
 
select * from PersonHowEducation prh inner join HowzeEducation he on 
              prh.HowzeEducationId=he.HowzeEducationId
              where he.HowzeEducationId=@@IDENTITY
 
what shall i realy do ?
Posted 5-Oct-12 19:37pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

In this case @@IDENTITY is not going to help you. To this job you need to add a column in the table named LastUpdatedDate with the Default Constraint of Current DateTime. While updating the table, you can update this column also. Then easily you can write select query based on that. Like this:
SELECT * FROM PersonHowEducation prh INNER JOIN HowzeEducation he ON
prh.HowzeEducationId=he.HowzeEducationId ORDER BY he.LastUpdatedDate DESC
 
This will give you a list of updated records in descending order of LastUpdatedDate column. You can customize the query according to your wish.
 

--Amit
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

U may try the SCOPE_IDENTITY which is purposed for exactly wat u want
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,108
1 OriginalGriff 8,186
2 CPallini 2,613
3 Richard MacCutchan 2,231
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100