When the database is set to Identity, there are three ways to get insert to the table:
Ident_current ('Table'): Returns the last Identity value generated by the specified table
@@ identity: Returns the last Identity value generated by any table in all ranges, all scope (Scope)
Scope_identity: Returns the last Identity value generated by any table generated in the current session and current scope
Explanation:
Assumptions INSERT to the two tables T1, T2 with the Identity column, respectively, the @@ identity value is the last Identity column value of T2. Ident_current ('T1') and Ident_current ('T2') are the last Identity column values of T1 and T2, respectively.
If the INSERT TRIGGER is written for T1, its content is inserted into T2. When the INSERT command is executed to the data to T1, then execute SELECT Scope_identity (), get the last Identity column value of the T1, although inserted data to T1 will trigger trigger SCOPEs are not in Insert Trigger in inserted data to T2, but the scope_current () command is not in Insert Trigger.
?
Identity () is usually used within Select ... INTO to create a new Identity listing in a new form. Grammar: Identity (Data Type [, Start Value, Value]). NEWID () returns the GUID value.
When you need to specify the IDENTITY value in the insert statement, you will use the following way: set identity_insert? Table Name on Insert INTO Table Name (ID, Column 1, Column 2) Values (0, Column Value 1, Column Value 2) Set Identity_Insert Name OFF