Insert the explicit value into the table's ID column

Source: Internet
Author: User

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_insertSet to fillDeleteStatement.


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

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.