After the SQL statement is used
Scope_identity ()
Of course you can also use SELECT @ @IDENTITY
But using the SELECT @ @IDENTITY is the go-to-global update. It is possible to obtain an incorrect value.
Example:
INSERT INTO dbo. Sns_blogdata (UserName) VALUES (' Santai ');
SELECT scope_identity ()
Gets the primary key return value for the insert into operation of the Sql-server database, scope_identity
After inserting a record, you want to get the return value of the primary key in its data table immediately. This primary key is generated automatically, in fact, there are many ways to implement, such as a query again, get out. Or to take the maximum value before inserting the data, add one to the maximum, and so on, many ways, but some are inconvenient.
The quickest way to feel personal is to get the value of the primary key directly after inserting the data, and then return.
Here's how:
The SQL statements are as follows:
INSERT into TableName (fieldname ...) VALUES (value ...) SELECT @ @IDENTITY as Returnname;
Add the SELECT @ @IDENTITY as returnname in the SQL statement; The value used to get the primary key.
SQL statement returns primary key scope_identity ()