如何获取在sql表中插入的最后/最近记录 [英] how to fetch last/recent record inserted in sql table

查看:55
本文介绍了如何获取在sql表中插入的最后/最近记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



伙计们我最后/最近插入到桌面上的记录被抓住了。



我尝试了max(recid)和@@ identity ...但是我的方法不起作用。



所以plzzz,建议我一些东西除此之外。



Plzz帮帮我。



谢谢

Hello everyone,

Guys i was stuck on fetching the record which was last/recently inserted into the table.

I have tried max(recid) and @@identity...but its not work on my approach.

so plzzz, suggest me something other then this.

Plzz help me.

Thanks

推荐答案

甚至不尝试。

您可以检索表格中的第一行(或最后一张视您的观点而定)行,但是...而且它很大但是。 ..only如果你绝对使用ORDER BY子句指定一个排序顺序 - 如果没有,SQL可以自由地返回当时适合它的任何顺序的记录 - 这可能在同一SELECT语句的连续执行之间有所不同。 />


如果您真的想要最后输入的记录,那么请确保包含时间戳字段并按顺序排序:

Don't even try.
You can retrieve the first (or last depending on your viewpoint) row in a table, but...and it's a big but...only if you absolutely specify a sort order with an ORDER BY clause - if not, SQL is at liberty to return records in any order appropriate to it at the time - which may vary between successive executions of the same SELECT statement.

If you genuinely want the last entered record, then make sure you include a timestamp field and order by that:
SELECT TOP 1 * FROM MyTable ORDER BY enterTime DESC

将返回它。



但是......我怀疑你没有。我怀疑你要么在插入后要么检索身份字段的值,要么试图获取将被分配给下一条记录的数字。

在后一种情况下,不要T。只是不要。它会导致更多问题,它会解决它。



在前一种情况下,你必须在INSERT操作中执行此操作:

will return it.

But...I suspect you don't. I suspect that you are either trying to retrieve the value of an Identity field after you INSERTed it, or are trying to get the number that will be assigned to the next record.
In the latter case, don't. Just don't. It'll cause more problems that it ever looks like it solves.

In the former case, you have to do that as part of the INSERT operation:

INSERT INTO MyTable (UserName) VALUES("Joe Shmoe"); SELECT SCOPE_IDENTITY()

并使用ExecuteScalar方法调用来获取记录ID 。 (或者更好的是,不要使用Identity行,而是使用GUID值并从C#代码提供它。)

And use a ExecuteScalar method call to fetch the record ID. (Or better, don't use Identity rows, use GUID values instead and supply it from the C# code).


如果您的表有标识列,则使用@@ IDENTITY。



如果没有,那么检查数字相关的数据类型(int,bigint等)并使用以下查询。





从< tablename>中选择前1 *按< number type =columnname =>排序说明



假设您的情况不同而且您不能使用上述内容。然后创建一个额外的列< inserttimestamp>并使用此列获取最后插入的记录。



如果采用不同的方式,请告诉我。
If your table has identity column, then use @@IDENTITY.

If do not have then check for number related datatype(int, bigint etc.) and use the below query.


Select top 1 * from <tablename> Order by <number type="" columnname=""> Desc.

Suppose your condition is different and you can't use the above. Then create one extra column <inserttimestamp> and use this column to get the last inserted record.

If different way to do it, let me know also.


SCOPE_IDENTITY

http://technet.microsoft.com/en-us/library /ms190315.aspx [ ^ ]将为您提供刚插入的那个。



如果您以后尝试这样做,您需要订购一些您可以确定为最新版本的东西。
SCOPE_IDENTITY
http://technet.microsoft.com/en-us/library/ms190315.aspx[^] will get you the one you just inserted.

If you are trying to do this later you'll need to order by something that you can be sure will be the latest.


这篇关于如何获取在sql表中插入的最后/最近记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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