sqlserver identitySummary

To insert a value into an automatic number (or identity column, IDENTITY), you need to set SET IDENTITY_INSERT

Example:

1. First create a table with an identity column:
CREATE TABLE products (id int IDENTITY PRIMARY KEY, product varchar (40))

2. Attempt to do the following in the table:
INSERT INTO products (id, product) VALUES(3, 'garden shovel')

results in an error: "Cannot insert into table 'products' when IDENTITY_INSERT is set to OFF The identity column in inserts explicit values."

3. Use instead:
SET IDENTITY_INSERT products ON
INSERT INTO products (id, product) VALUES(1, 'garden shovel')

returns correct.

4. Create another table products2 and try the same insert operation:
CREATE TABLE products2 (id int IDENTITY PRIMARY KEY, product varchar(40))

and then execute:
SET IDENTITY_INSERT products2 ON
INSERT INTO products2 (id, product) VALUES(1, 'garden shovel')

Results in error: "IDENTITY_INSERT for table 'material.dbo.products' is already ON. Cannot perform SET operation on table 'products2'."

instead:
SET IDENTITY_INSERT products OFF
SET IDENTITY_INSERT products2 ON
INSERT INTO products2 (id, product) VALUES (2, 'garden shovel')

passed.

5. Try the following:
SET IDENTITY_INSERT products2 ON
INSERT INTO products2 SELECT * FROM products

results in an error: "An explicit value can only be specified for an identity column in table 'products2' if a list of columns is used and IDENTITY_INSERT is ON. ”

6. Change to:
SET IDENTITY_INSERT products2 ON
INSERT INTO products2 (id, product) SELECT * FROM products

is executed through.

Summary:

1. At any time in each connection session, IDENTITY_INSERT ON can only be set for one table, and the setting is only valid for the current session;
2. When performing an insert operation on an identity column, be sure to list the identity column ( Of course, other related columns need to be listed at the same time).

P.S.
SQL Server help documentation related content

SET IDENTITY_INSERT
allows explicit values ​​to be inserted into the identity column of a table.

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

parameter
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 identity column.

Remarks Only one table in a session can have the IDENTITY_INSERT property set to ON at
any time. If a table has this property set to ON, and a SET IDENTITY_INSERT ON statement is issued for another table, Microsoft® SQL Server™ returns an error that SET IDENTITY_INSERT is set to ON and reports that this property is set to ON table.

If the inserted value is greater than the current identity value of the table, SQL Server automatically uses the newly inserted value as the current identity value.

The setting for SET IDENTITY_INSERT is set at execution or runtime, not at parse time.

Permissions
Execute permissions default to the sysadmin fixed server role and the db_owner and db_ddladmin fixed database roles and object owner.

Example
The following example creates a table with an identity column and shows how to use the SET IDENTITY_INSERT setting to fill in the gaps in the identity 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;
-- should 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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326919030&siteId=291194637