从bigint将列的数据类型更改为uniqueidentifier

时间:2022-10-06 16:43:29

I want to change the datatype of a column in a table in sql server. I used the following statement:

我想更改sql server中表的列的数据类型。我使用了以下声明:

ALTER TABLE dbo.tbltest  
ALTER COLUMN ID uniqueidentifier

But it throws the error

但它会引发错误

Operand type *: bigint is incompatible with uniqueidentifier

操作数类型冲突:bigint与uniqueidentifier不兼容

4 个解决方案

#1


25  

Follow the following Steps:-

请遵循以下步骤: -

1).Firstly delete old data from the table.

1)。首先从表中删除旧数据。

2). Use ALTER TABLE dbo.tbltest
ALTER COLUMN ID varchar(200)

2)。使用ALTER TABLE dbo.tbltest ALTER COLUMN ID varchar(200)

3). Now again ALTER TABLE dbo.tbltest
ALTER COLUMN ID uniqueidentifier

3)。现在再次ALTER TABLE dbo.tbltest ALTER COLUMN ID uniqueidentifier

To be clear, you can't convert a column from numeric to uniqueidentifier directly,

要清楚,您不能直接将列从numeric转换为uniqueidentifier,

but you can convert numeric -> varchar -> uniqueidentifier.

但你可以转换数字 - > varchar - > uniqueidentifier。

#2


4  

You need to add the column first (possibly with a default or nullable so that the column can be successfully added to rows with existing data), update it to the values you want or need, change any keys (if it is a primary key change it to the new column and then also change any foreign keys to this table), then remove the old column and alter the new column as necessary (e.g. setting identity seed, removing nullable, etc.).

您需要先添加列(可能使用默认值或可以为空,以便可以将列成功添加到包含现有数据的行),将其更新为您想要或需要的值,更改任何键(如果它是主键更改)它到新列,然后还更改此表的任何外键),然后删除旧列并根据需要更改新列(例如设置标识种子,删除可空等)。

#3


3  

Note to people using Entity Framework:
Obviously, if you have a large database with complex relationships, dropping all relationships and ID columns, and re-adding them is a huge ordeal.

对使用实体框架的人员的注意事项:显然,如果您拥有一个具有复杂关系的大型数据库,删除所有关系和ID列,并重新添加它们是一个巨大的考验。

It's much easier to update the generated model (.edmx).

Yes, Visual Studio allows you to update the data type without complaining. In fact, when you save the .edmx file, the relationships will be validated; any fields that you may have missed will show up as errors.

更新生成的模型(.edmx)要容易得多。是的,Visual Studio允许您在不抱怨的情况下更新数据类型。实际上,当您保存.edmx文件时,将验证关系;您可能错过的任何字段都会显示为错误。

After you're finished, you can regenerate the database from the model. Boom, done.

完成后,您可以从模型中重新生成数据库。热潮,完成了。

#4


0  

If Singh's answer is not working then you probably have to drop the the table and recreate it.

如果Singh的答案不起作用,那么你可能不得不放弃表并重新创建它。

#1


25  

Follow the following Steps:-

请遵循以下步骤: -

1).Firstly delete old data from the table.

1)。首先从表中删除旧数据。

2). Use ALTER TABLE dbo.tbltest
ALTER COLUMN ID varchar(200)

2)。使用ALTER TABLE dbo.tbltest ALTER COLUMN ID varchar(200)

3). Now again ALTER TABLE dbo.tbltest
ALTER COLUMN ID uniqueidentifier

3)。现在再次ALTER TABLE dbo.tbltest ALTER COLUMN ID uniqueidentifier

To be clear, you can't convert a column from numeric to uniqueidentifier directly,

要清楚,您不能直接将列从numeric转换为uniqueidentifier,

but you can convert numeric -> varchar -> uniqueidentifier.

但你可以转换数字 - > varchar - > uniqueidentifier。

#2


4  

You need to add the column first (possibly with a default or nullable so that the column can be successfully added to rows with existing data), update it to the values you want or need, change any keys (if it is a primary key change it to the new column and then also change any foreign keys to this table), then remove the old column and alter the new column as necessary (e.g. setting identity seed, removing nullable, etc.).

您需要先添加列(可能使用默认值或可以为空,以便可以将列成功添加到包含现有数据的行),将其更新为您想要或需要的值,更改任何键(如果它是主键更改)它到新列,然后还更改此表的任何外键),然后删除旧列并根据需要更改新列(例如设置标识种子,删除可空等)。

#3


3  

Note to people using Entity Framework:
Obviously, if you have a large database with complex relationships, dropping all relationships and ID columns, and re-adding them is a huge ordeal.

对使用实体框架的人员的注意事项:显然,如果您拥有一个具有复杂关系的大型数据库,删除所有关系和ID列,并重新添加它们是一个巨大的考验。

It's much easier to update the generated model (.edmx).

Yes, Visual Studio allows you to update the data type without complaining. In fact, when you save the .edmx file, the relationships will be validated; any fields that you may have missed will show up as errors.

更新生成的模型(.edmx)要容易得多。是的,Visual Studio允许您在不抱怨的情况下更新数据类型。实际上,当您保存.edmx文件时,将验证关系;您可能错过的任何字段都会显示为错误。

After you're finished, you can regenerate the database from the model. Boom, done.

完成后,您可以从模型中重新生成数据库。热潮,完成了。

#4


0  

If Singh's answer is not working then you probably have to drop the the table and recreate it.

如果Singh的答案不起作用,那么你可能不得不放弃表并重新创建它。