For example, we created a new table T_user, the fields are as follows u_id, the primary key, the designator, user_name ...
Then we'll perform a new insert operation:
Insert into T_user (USER_NAME,USER_PASSWORD,USER_EMAIL,USER_IP)
VALUES (' admin ', ' 123456 ', ' 32962435@qq.com ', ' 58.20.158.20 ');
One day we wanted to get the value of the u_id to insert this data when we added the new data, and we knew that a new output from MSSQL2005 would be used to enter a value that we could use to implement
There are two kinds of methods, one is to enter a field directly, such as:
Insert into T_user (user_name,user_password,user_email,user_ip) output inserted.u_id///output inserted.u_id must be placed in the Values, can not be placed at the end of the SQL statement, otherwise it is wrong, inserted is fixed, you want to enter a field currently inserted in the back of the field can be, we want to get the u_id so is inserted.u_id
VALUES (' admin ', ' 123456 ', ' 32962435@qq.com ', ' 58.20.158.20 ');
Another method is to use the @ @identity, @ @identity is a global variable built into the system, its left and right to enter the last identifier, we can go two steps can also be implemented in one step
The following code is implemented in one step
Insert into T_user (USER_NAME,USER_PASSWORD,USER_EMAIL,USER_IP) output @ @identity
VALUES (' admin ', ' 123456 ', ' 32962435@qq.com ', ' 58.20.158.20 ');