Explicit values can be inserted into the table's ID column.
|
Set identity_insert [database_name. [Schema_name].] Table {on | off} |
At any time, the identity_insert attribute of only one table in a session can be set to on. If a table has set this attribute to on, SQL Server 2005 returns an error message when the set identity_insert on statement is sent to the other table, indicating that set identity_insert is set to on, the report shows the table whose attribute is 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.
-
Database_name
-
The name of the database where the specified table is located.
-
Schema_name
-
The name of the schema to which the table belongs.
-
Table
-
The name of the table that contains the ID column.
The user must be the object owner, a member of the SysAdmin fixed server role, or a member of the db_owner or db_ddladmin fixed database role.
The following example creates a table containing the ID column and shows how to useSet identity_insert
Set to fillDelete
Statement.
|
Copy code |
Use adventureworks; go -- create tool table. create Table DBO. tool (ID int identity not null primary key, name varchar (40) not null) Go -- inserting values into products table. insert into DBO. tool (name) values ('screwdriver ') insert into DBO. tool (name) values ('hammer ') insert into DBO. tool (name) values ('saw ') insert into DBO. tool (name) values ('shovel') Go -- create a gap in the identity values. delete DBO. tool where name = 'saw 'goselect * From DBO. toolgo -- try to insert an explicit id value of 3; -- shocould return a warning. insert into DBO. tool (ID, name) values (3, 'garden shovel') Go -- set identity_insert to on. set identity_insert DBO. tool ongo -- try to insert an explicit id value of 3. insert into DBO. tool (ID, name) values (3, 'garden shovel') goselect * From DBO. toolgo -- drop Products table. drop table DBO. toolgo |