如何从表中检索最新插入或更新的标识? [英] how to retrieve the latest inserted or updated identity from a table?

查看:67
本文介绍了如何从表中检索最新插入或更新的标识?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.
我想从表中检索最新插入的或更新的标识.但是,每当我编写以下代码时,根本没有结果.

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 ?

推荐答案

在这种情况下,@@ IDENTITY不会为您提供帮助.为此,您需要在名为LastUpdatedDate的表中添加一列,并将其列为当前日期时间的默认约束.在更新表时,您也可以更新此列.然后,您可以轻松地基于此编写选择查询.像这样:
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



这将以LastUpdatedDate列的降序为您提供更新记录的列表.您可以根据自己的需求自定义查询.


--Amit



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


U可以尝试SCOPE_IDENTITY,该SCOPE_IDENTITY用于恰好是您想要的
U may try the SCOPE_IDENTITY which is purposed for exactly wat u want


这篇关于如何从表中检索最新插入或更新的标识?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆