After insert into, get the automatically inserted ID (select @ identity)

Source: Internet
Author: User

Just now I saw a friend in the blog Park

 

// Locate the id value just inserted

Datatable dt = Helper. getdatatable ( " Select top 1 msg_id from tb_message_board order by msg_time DESC " );

poor experience, especially when concurrency occurs. Search by yourself for future use.

after the insert statement is run, execute select @ identity to obtain the automatically generated id

if it is SQL server, it is best to use select scope_identity () as id
because @ identity global

similar ident_current ('table')

ident_current returns the id value generated at the end of any session and a specific table in any scope. Ident_current is not restricted by the scope and session, but by the specified table. Ident_current returns the value generated for a specific table in any session and scope.
@ identity returns the value of the last identifier generated for any table in all scopes of the current session.
scope_identity returns the last generated id value for the current session and any table in the current scope

scope_identity and @ identity returned in the current session the last Identifier value generated in any table. However, scope_identity only returns the value inserted into the current scope; @ identity is not limited to a specific scope.

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.