且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在SQL Server 2008中的自动生成的列中插入相同的值

更新时间:2022-06-20 17:49:13

坏主意.

自动生成的列的整个想法是,它保留唯一值,而不是唯一的-当我感觉类似它值时.
"ID"字段的全部目的是唯一地标识一行,以便可以对其进行正确处理.对于您的方案,尝试删除第二行也可能会删除第三行-这可能不是您想要的.

保留ID单独,使其唯一,然后重新考虑您认为需要重复的行ID的所有操作.

您想达到什么目的,以为这是个好主意?

[edit]次要错字-OriginalGriff [/edit]
Bad idea.

The whole idea of a autogenerated column is that it holds unique values, not unique-except-when-I-feel-like-it values.
The whole purpose of an "ID" field is to uniquely identify a row, so that it can be handled correctly. With your scheme, an attempt to delete the second row could well delete the third as well - which is probably not what you want.

Leave IDs alone, make them unique and reconsider whatever you are doing that you think needs duplicate row ids.

What are you trying to achieve that you think this would be a good idea?

[edit]Minor typos - OriginalGriff[/edit]


您需要创建表而不将列指定为标识,然后插入所需的值,然后将列更改为是一个身份,种子的起始地址不是1,而是5(在您的示例中)
YOu need to create the table without the column being specified as an identity, then insert the values you want, and then change teh column to be an identity, with the seed starting not at 1, but at 5 (in your example)


您可以使用
You can use
SET IDENTITY_INSERT [ database. [ owner. ] ] { table } { ON | OFF }


将值插入标识列的语句.

有关详细信息,请访问 http://msdn.microsoft.com/en-us/library/aa259221(SQL.80).aspx [ ^ ]


statement to insert a value into identity column.

Detailed information is available at http://msdn.microsoft.com/en-us/library/aa259221(SQL.80).aspx[^]