How to get last inserted row in sql server

How can I get the last inserted row if there is no column in the table Uniqueidentifier or identity.

I'm waiting for your good idea.

+3
source share
6 answers

You need to somehow determine the order of the lines in order to determine this. Something like the Creation Date or IDENTIFICATION column.

+4
source

You can not. The table consists of an unordered set of rows *. If you need to know, for example, when a row was inserted, you need to add this information to the table definition (adding a new column) and fill it accordingly.

* , . , ( )

+2

uniqueIdentifier identity, insert.

+2

, . - . ID, DateTime.. , .

0

if you insert something using a stored procedure, you should use SELECT SCOPE_IDENTITY (), which will return the last authentication value created in the current session, but it will also limit it to the current scope. With this use, you will not get the last inserted identifier that could be triggered by the trigger.

Keep this in mind if you will use the result for an insert operation inside the same or called procedure.

0
source
DECLARE @IDs TABLE(id int)

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

- inserted identification will be in @IDstable variable

-1
source

Source: https://habr.com/ru/post/1784719/


All Articles