How to get the value of the self-increment ID when inserting the current statement

Source: Internet
Author: User

Since it is a self-increasing data column,

Then the data inserted at this point should be the last one, and its ID should be the largest

You can use select Max (ID) from table;

But there is a problem, if it is a data table that is being used with frequent inserts,

After you execute your INSERT statement, the ID of your select is not necessarily the ID of your statement until you execute the SELECT statement, and if another user inserts again.

All that should be used is

Inset into table name (column name, column name,.. ) Output inserted.id values (data, data, ...) ;


Examples of use:
1. For insert, you can refer to the inserted table to query the properties of the new row .
insert into [table name] (a) OUTPUT inserted.a values (' a ')
2. For delete, you can reference the deleted table to query the properties of the old row .
Delete [table name] OUTPUT deleted.a where links = ' a '
3. For update, use the deleted table to query the properties of the updated row before the change, using the inserted table to identify the changed value of the row being updated.
Update [table name] Set a = ' B ' OUTPUT inserted.a where a = ' a ' (returns the modified value)
Update [table name] Set a = ' B ' OUTPUT deleted.a where a = ' a ' (returns the value before modification)


How to get the value of the self-increment ID when inserting the current statement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.