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

问题描述:

我想更改 sql server 中表中列的数据类型.我使用了以下语句:

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

ALTER TABLE dbo.tbltest  
ALTER COLUMN ID uniqueidentifier

但它抛出错误

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

Operand type clash: bigint is incompatible with uniqueidentifier

不能从整数转换为 uniqueidentifier.但你可以这样做.

You cannot convert from an integer to a uniqueidentifier. But you can do it like this.

  1. 首先从表中删除旧数据.

  1. First delete old data from the table.

将列更改为某种文本格式(例如 VARCHAR(200)).

Alter the column to some text-format (such as VARCHAR(200)).

ALTER TABLE dbo.tbltest  
ALTER COLUMN ID VARCHAR(200)

  • 又来了

  • Now again
    ALTER TABLE dbo.tbltest  
    ALTER COLUMN ID uniqueidentifier
    

  • 需要明确的是,您不能直接将列从数字转换为uniqueidentifier,但可以将numeric 转换为varchar 为唯一标识符代码>.

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