Вопрос

I got a problem. I use SQL Server 2008 Express.

When new row is inserted into a table the IDENT_CURRENT function returns correct value (last inserted row id). But when the last inserted row is deleted (DELETE FROM REPORT WHERE ID='last_inserted_id') the IDENT_CURRENT function returns id of the deleted row. It do not update.

I need to return the last id from the table.

SELECT IDENT_CURRENT('report')
Это было полезно?

Решение

This is working as expected. The function returns the last identity value used, even if it's been deleted. Here's an excerpt from Microsoft's documentation:

IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope.

Другие советы

Identity always goes up. It won't go down if you delete rows. Plus it can have 'blanks' in values. Read this for detailes http://technet.microsoft.com/en-us/library/ms186775.aspx

If you want the max ID you can find in your table, just use

SELECT MAX(ID) FROM table
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top