Return last value entered in the table where the value is not identity?

7

I would like to return the last value of the primary key inserted in the table, but my primary key is CPF so I can not use SELECT SCOPE_IDENTITY() .

    
asked by anonymous 24.04.2017 / 16:24

1 answer

7

There is no T-Sql for this. You only use SCOPE_IDENTITY or IDENT_CURRENT as long as the column is Identity.

I recommend the following readings:

An alternative is to add a DataInsercao field with type datetime to the structure of your table. By default this field should receive the current date, the GETDATE () can be useful in this case. This way you can sort by the most current date and check the last record inserted.

Select should be something like this:

Select top 1 * from MinhaTabela Order By DataInsercao DESC
    
24.04.2017 / 16:47