如何以编程方式将非身份列更改为身份一?

发布于 2024-07-17 08:29:31 字数 209 浏览 0 评论 0原文

我有一个表,其列 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

念﹏祤嫣 2024-07-24 08:29:31

据我所知,您必须创建一个临时表,并将ID字段创建为IDENTITY,然后从原始表中复制所有数据。 最后,删除原始表并重命名临时表。 这是一个表(名为 TestTable)的示例,该表仅包含一个名为 ID(整数,非 IDENTITY)的字段:

BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_TestTable
    (
    ID int NOT NULL IDENTITY (1, 1)
    )  ON [PRIMARY]
GO
SET IDENTITY_INSERT dbo.Tmp_TestTable ON
GO
IF EXISTS(SELECT * FROM dbo.TestTable)
     EXEC('INSERT INTO dbo.Tmp_TestTable (ID)
        SELECT ID FROM dbo.TestTable WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_TestTable OFF
GO
DROP TABLE dbo.TestTable
GO
EXECUTE sp_rename N'dbo.Tmp_TestTable', N'TestTable', 'OBJECT' 
GO
COMMIT

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):

BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_TestTable
    (
    ID int NOT NULL IDENTITY (1, 1)
    )  ON [PRIMARY]
GO
SET IDENTITY_INSERT dbo.Tmp_TestTable ON
GO
IF EXISTS(SELECT * FROM dbo.TestTable)
     EXEC('INSERT INTO dbo.Tmp_TestTable (ID)
        SELECT ID FROM dbo.TestTable WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_TestTable OFF
GO
DROP TABLE dbo.TestTable
GO
EXECUTE sp_rename N'dbo.Tmp_TestTable', N'TestTable', 'OBJECT' 
GO
COMMIT
一个人的夜不怕黑 2024-07-24 08:29:31

看起来 SQL Mobile 支持更改列标识,但在 SQL Server 2005 上不喜欢 BOL 中的示例。

因此,您的选择是使用标识列创建一个新的临时表,然后打开标识插入:

Create Table Tmp_MyTable ( Id int identity....)

SET IDENTITY_INSERT dbo.Tmp_Category ON

INSERT Into Tmp_MyTable (...)
Select From MyTable ....

Drop Table myTable

EXECUTE sp_rename N'dbo.Tmp_MyTable', N'MyTable', 'OBJECT' 

此外,您可以尝试首先将该列添加为标识列,然后打开标识插入。 然后删除原始列。 但我不确定这是否有效。

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:

Create Table Tmp_MyTable ( Id int identity....)

SET IDENTITY_INSERT dbo.Tmp_Category ON

INSERT Into Tmp_MyTable (...)
Select From MyTable ....

Drop Table myTable

EXECUTE sp_rename N'dbo.Tmp_MyTable', N'MyTable', 'OBJECT' 

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.

墨小沫ゞ 2024-07-24 08:29:31

猜测您的任务运气不佳......

在表设计中,您应该能够进入属性并在“身份规范”下将“身份规范”更改为“是”,并分配列主键(如果它以前具有主键)钥匙。

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.

本宫微胖 2024-07-24 08:29:31

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文