Change datatype of column to uniqueidentifier from bigint

后端 未结 5 1007
遇见更好的自我
遇见更好的自我 2021-01-01 19:05

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 uniqueidentifie         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-01 20:04

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

    1. First delete old data from the table.

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

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

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

提交回复
热议问题