In recent database programming, you need to retrieve the value of the auto-increment column after executing SQL insert. It is easy to get this value in the database: return the last inserted record
You can use SELECT IDENT_CURRENT ('tablename') for automatic numbering.
You can also use the following code:
The code is as follows: |
Copy code |
Select @ identity |
Execute this statement after insert. On the internet, someone added a sentence before insert:
The code is as follows: |
Copy code |
Set nocount on; |
This statement does not return the queried row count.
So:
The code is as follows: |
Copy code |
Set nocount on; Insert into tbUser (a, B) values ("a", "B "); Select @ identity; |
Of course, you can add a trigger.
The code is as follows: |
Copy code |
Create trigger trUser_insert on tbUser for insert
Select @ identity Go |
Return the next automatic number:
SELECT IDENT_CURRENT ('tablename') + (SELECT IDENT_INCR ('tablename '))
SELECT @ IDENTITY -- returns the last tag value generated in all tables of the current session.