Database primary key self-increment insert display value

Source: Internet
Author: User

SQL Server database primary key self-increment insert display value

A few days ago at work encountered in the deletion of the database table data, when deleted, the re-added data did not get the original data ID value (the table ID is the primary key, and set to self-increment), using SQL Server 2008, is now resolved, and everyone share!

Specific situation:

1. Create the table T_test, set the primary key auto-increment, as


2. Inserting data into the table

because the primary key field ID in the table is self-increment, you do not need to specify a display insert when inserting, so the SQL statement is

<span style= "FONT-SIZE:24PX;" >insert into  [xxx].[ DBO]. [T_test]  VALUES (' xiaoming ') insert into  [xxx].[ DBO]. [T_test]  VALUES (' Hanmei ') insert into  [xxx].[ DBO]. [T_test]  VALUES (' Lilei ') insert into  [xxx].[ DBO]. [T_test]  VALUES (' Ligang ') insert into  [xxx].[ DBO]. [T_test]  VALUES (' Xiaozhi ') </span>

When inserted, the Display Value ID field is not specified, the database automatically increases the primary key ID value, and the data in the database is:

3. Delete data and re-add data with display values

<span style= "FONT-SIZE:24PX;" >delete [xxx]. [dbo]. [t_test] WHERE id = 1</span>
Insert data with display values:

<span style= "FONT-SIZE:24PX;" >insert into  [guagua_new_event_system_test].[ DBO]. [T_test] VALUES (1, ' xiaoming ') </span>
Database hints:

Msg 8101, Level 16, State 1, line 1th
You can specify an explicit value for the Identity column in table ' Xxx.dbo.t_test ' only if the column list is used and Identity_insert is on.

When resetting Indentity_insert to on, re-add the SQL statement as:

<span style= "FONT-SIZE:18PX;" > SET  identity_insert [xxx].[ DBO]. [T_test]  On </span>
<span style= "FONT-SIZE:18PX;" > INSERT INTO  [xxx].[ DBO]. [T_test]  VALUES (1, ' xiaoming ') </span>
Database hints:

Msg 8101, Level 16, State 1, line 2nd
You can specify an explicit value for the Identity column in table ' Xxx.dbo.t_test ' only if the column list is used and Identity_insert is on.
Clearly already set the Indentity_insert for on, but why did not add in, look at the SQL Server 2008 Help document, only understand the need to make one by one corresponding column name when the display insert.

So, the correct SQL statement is:

<span style= "FONT-SIZE:18PX;" >set  Identity_insert [xxx].[ DBO]. [T_test]  On insert INTO  [xxx].[ DBO]. [T_test] (ID, name)  VALUES (1, ' xiaoming ') SET identity_insert [xxx]. [dbo]. [T_test] Off</span>

Just when the insertion value is displayed, you need to make a column name and open the Indentity_insert that allows the insertion to be inserted!

------long way to repair, I will go up and down and quest



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.