How to accurately obtain the id value in SQL Server (1)

Source: Internet
Author: User

SQL Server has three different functions that can be used to obtain the final id value in the table containing the ID column:

 
 
  1. @ IDENTITY
  2. SCOPE_IDENTITY ()
  3. IDENT_CURRENT ('Data table name')

Although all the above three functions can return the value of the inserted ID column generated by the database engine, they vary depending on the source of the inserted row (for example, stored procedure or trigger) and the connection to insert the row, these three functions have different functions.

The @ IDENTITY function returns the id value (including any called stored procedures and triggers) generated when the current connection is inserted in all ranges ). This function is not only applicable to tables. The value returned by the function is the ID value generated by inserting rows in the last table.

The SCOPE_IDENTITY () function is almost the same as a function. The difference is that the value returned by the former is limited to the current range (that is, the stored procedure in execution ).

The last is the IDENT_CURRENT function, which can be used for all ranges and connections to obtain the final table id value. Unlike the previous two functions, this function is only used for tables and uses [data table name] as a parameter.

We can take an example to demonstrate how the above functions work.

First, we create two simple example tables: one representing the customer table and the other representing the audit table. The purpose of creating an audit table is to track all records of information inserted and deleted in the database.

The following is a reference clip:

 
 
  1. CREATE TABLE dbo.customer   
  2. (customerid INT IDENTITY(1,1) PRIMARY KEY)   
  3. GO   
  4. CREATE TABLE dbo.auditlog   
  5. (auditlogid INT IDENTITY(1,1) PRIMARY KEY,   
  6. customerid INT, action CHAR(1),   
  7. changedate datetime DEFAULT GETDATE())   
  8. GO 

Then, we need to create a stored procedure and a secondary trigger. This stored procedure inserts a new customer row into the database table and returns the generated id value, the trigger inserts rows into the audit table:

The following is a reference clip:

 
 
  1. CREATE PROCEDURE dbo.p_InsertCustomer @customerid INT output   
  2. AS   
  3. SET nocount ON   
  4. INSERT INTO dbo.customer DEFAULT VALUES   
  5. SELECT @customerid = @@identity   
  6. GO   
  7. CREATE TRIGGER dbo.tr_customer_log ON dbo.customer   
  8. FOR INSERT, DELETE   
  9. AS   
  10. IF EXISTS (SELECT 'x' FROM inserted)   
  11. INSERT INTO dbo.auditlog (customerid, action)   
  12. SELECT customerid, 'I'   
  13. FROM inserted   
  14. ELSE   
  15. IF EXISTS (SELECT 'x' FROM deleted)   
  16. INSERT INTO dbo.auditlog (customerid, action)   
  17. SELECT customerid, 'D'   
  18. FROM deleted   
  19. GO 

Now we can execute the program to create the first line of the customer table. The following is a reference segment:

 
 
  1. DECLARE @customerid INT   
  2. EXEC dbo.p_InsertCustomer @customerid output   
  3. SELECT @customerid AS customerid 


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.