如何以编程方式将非身份列更改为身份一?
我有一个表,其列 ID 为标识一。 接下来,我创建新的非身份列 new_ID 并使用 ID 列 + 1 中的值更新它。如下所示:
new_ID = ID + 1
接下来,我删除 ID 列并将 new_ID 重命名为“ID”。
以及如何在这个新列“ID”上设置身份?
我想以编程方式执行此操作!
I have a table with column ID that is identity one. Next I create new non-identity column new_ID and update it with values from ID column + 1. Like this:
new_ID = ID + 1
Next I drop ID column and rename new_ID to name 'ID'.
And how to set Identity on this new column 'ID'?
I would like to do this programmatically!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
据我所知,您必须创建一个临时表,并将ID字段创建为IDENTITY,然后从原始表中复制所有数据。 最后,删除原始表并重命名临时表。 这是一个表(名为 TestTable)的示例,该表仅包含一个名为 ID(整数,非 IDENTITY)的字段:
As far as I know, you have to create a temporary table with the ID field created as IDENTITY, then copy all the data from the original table. Finally, you drop the original table and rename the temporary one. This is an example with a table (named TestTable) that contains only one field, called ID (integer, non IDENTITY):
看起来 SQL Mobile 支持更改列标识,但在 SQL Server 2005 上不喜欢 BOL 中的示例。
因此,您的选择是使用标识列创建一个新的临时表,然后打开标识插入:
此外,您可以尝试首先将该列添加为标识列,然后打开标识插入。 然后删除原始列。 但我不确定这是否有效。
Looks like SQL Mobile supports altering a columns identity but on SQL Server 2005 didn't like the example from BOL.
So your options are to create a new temporary table with the identity column, then turn Identity Insert on:
Additionally you can try and add the column as an identity column in the first place and then turn identity insert on. Then drop the original column. But I am not sure if this will work.
猜测您的任务运气不佳......
在表设计中,您应该能够进入属性并在“身份规范”下将“身份规范”更改为“是”,并分配列主键(如果它以前具有主键)钥匙。
Guessing you didn't have much luck with your task....
In table design, you should be able to go into properties and under Identity Specification change (Is Identity) to Yes and assign the column primary key if it formerly had the primary key.
来自 SqlServerCentral.com
从 Non-IDENTITY 更改为 IDENTITY,反之亦然
From SqlServerCentral.com
Changing from Non-IDENTITY to IDENTITY and vice versa
Identity 是在创建表或在 alter table 语句中添加新列时设置的属性。 您无法更改该列并将其设置为标识,并且同一个表中不可能有两个标识列。
根据表的大小,是否可以简单地创建一个新表? 复制旧身份列的架构,然后使用 SET IDENTITY_INSERT ON 使用旧身份列中所需的内容填充新身份列。
An Identity is a property that is set at the time the table is created or a new column is added in alter table statement. You can't alter the column and set it to identity and it is impossible to have two identity columns within the same table.
Depending on the size of the table, is it possible to simply create a new table? copy over the schema of the old one and then use SET IDENTITY_INSERT ON to populate the new identity column with what you want from the old one.