Insert a field type is automatically incremental import data

Source: Internet
Author: User
SET IDENTITY_INSERT Table ON

SET IDENTITY_INSERT
Explicit values can be inserted into the table's ID column.

Syntax
SET IDENTITY_INSERT [database. [owner.] {table} {ON | OFF}

Parameters
Database

Is the name of the database where the specified table resides.

Owner

Is the name of the table owner.

Table

Is the name of the table containing the ID column.

Note
At any time, the IDENTITY_INSERT attribute of only one table in the session can be set to ON. If a table has SET this attribute to ON and sends a SET IDENTITY_INSERT ON statement to the other table, Microsoft? SQL Server? An error message is returned, indicating that SET IDENTITY_INSERT has been SET to ON and that this attribute has been SET to ON.

If the inserted value is greater than the current table id value, SQL Server automatically uses the new value as the current ID value.

SET IDENTITY_INSERT is SET during execution or running, rather than during analysis.

Permission
By default, the execution permission is granted to the sysadmin fixed server role, db_owner, db_ddladmin fixed database role, and object owner.

Example
The following example creates a table with an ID column and shows how to use the SET IDENTITY_INSERT setting to fill gaps in the ID values caused by the DELETE statement.

-- Create products table.
Create table products (id int identity primary key, product varchar (40 ))
GO
-- Inserting values into products table.
Insert into products (product) VALUES ('screwdriver ')
Insert into products (product) VALUES ('hammer ')
Insert into products (product) VALUES ('saw ')
Insert into products (product) VALUES ('shovel ')
GO

-- Create a gap in the identity values.
DELETE products
WHERE product = 'saw'
GO

SELECT *
FROM products
GO

-- Attempt to insert an explicit ID value of 3;
-- Shoshould return a warning.
Insert into products (id, product) VALUES (3, 'garden shovel ')
GO
-- SET IDENTITY_INSERT to ON.
SET IDENTITY_INSERT products ON
GO

-- Attempt to insert an explicit ID value of 3
Insert into products (id, product) VALUES (3, 'garden shovel ').
GO

SELECT *
FROM products
GO
-- Drop products table.
Drop table products
GO

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.