In sql server, i created a table with column id as int with identity, second one is name varchar, i created a proc for inserting and i write to statement that after inserting record i want that inserted record id for that i use @@identity it is working but, strangely @@identity is showing more rows, but in table that much records are not there shwoing less recrords. my proc is
alter proc usp_test123
as begin
set nocount on;
insert into test123 values('sasi')
select @@IDENTITY
end
Do you mean that the returned identity could be 10, while there are only 8 rows in the table?
identitycolumns are not guaranteed to be contigous. If rows are deleted, or a transaction doing an insert is rolled back, there will be missing numbers in the series.As a side note, you should use
SCOPE_IDENTITY()instead of@@IDENTITY.@@IDENTITYreturns the id of the latest row inserted. If there are triggers on the table that you insert into, you would get the latest row inserted by the trigger.SCOPE_IDENTITY()gets the latest identity generated in the same code scope.