T-SQL inserts a value into the auto-increment Field

Source: Internet
Author: User

--- I have done this before, but I have encountered it in the project, but I cannot remember it at half past one ~~~

Find http://hi.baidu.com/grrc/blog/item/16d216a85b61bbb6ca130c13.html.Article.

Now, I will extract the content of this article for your note:

Generally, values cannot be inserted into the SQL Server auto-increment field. If this is the case, SQL Server will give you an error warning:

Server: MSG 544, level 16, state 1, line 1 cannot insert explicit value for identity column in table 't'when identity_insert is set to off.

This error message prompts that if you insert a value to the SQL Server field, you need to set the identity_insert option to on.

Set identity_insert on

Let's look at a specific example:

Create Table DBO. t (ID int identity (1, 1) not null, name varchar (50 ))
Set identity_insert t oninsert into T (ID, name) values (1, 'sqlstudy ') set identity_insert t off

Note that after the auto-increment field is inserted, you must set identity_insert to off in time.

 

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.