如何在SQL Server 2008中的自动生成的列中插入相同的值 [英] how to insert same value in auto generated column in sql server 2008

查看:136
本文介绍了如何在SQL Server 2008中的自动生成的列中插入相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

how to insert same value in auto generated column in sql server 2008



我的表格脚本是:



my table script is:

CREATE TABLE [dbo].[rr](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [name] [varchar](50) NULL,
    [add] [varchar](50) NULL
)




我想要这样的表值




i want my table value like that

id      name    address

1	aa      noida
2	ss	delhi
2	dd      punjab
3	ee	okhla

推荐答案

坏主意.

自动生成的列的整个想法是,它保留唯一值,而不是唯一的-当我感觉类似它值时.
"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[^]


这篇关于如何在SQL Server 2008中的自动生成的列中插入相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆