开发者

How to get last inserted row in sql server

How can i get Last inser开发者_如何学Cted row when in table there is no Uniqueidentifier or identity column.

I am waiting of your good idea's.


You need some way of being able to identify the ordering of the rows to determine that. Something like a "creation date" or IDENTITY column.


If you want to get last row from table without uniqueIdentifier or identity then you can use insert trigger.


You can't. A table consists of an unordered set of rows*. If you need to know, for instance, when a row was inserted, you need to add that information into the table definition (by adding a new column) and populating it appropriately.

*even in the face of a clustered index, it's healthier to always consider a table as being an unordered set of rows. A clustered index is useful, but it doesn't guarantee any particular physical ordering (As in Martin's comment to this answer)


Don't believe you can. You'll need some kind of identifier to extract data. ID, DateTime.. anything that will always be different from the others.


if you insert something by stored procedure you should use SELECT SCOPE_IDENTITY() which will return the last identity value created in the current session, but it will also limit it to your current scope. By this usage you will not get last inserted identity which can be caused by trigger.

Have that in mind if you will use result for operation of insertion inside same or called procedure.


DECLARE @IDs TABLE(id int)

insert into TableName(TableID, TableRowValue)
output inserted.TableID into @IDs
values(857, 'Test');

-- The inserted identity will be in the @IDs table variable

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜