Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change datatype of column to uniqueidentifier from bigint

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

But it throws the error

Operand type clash: bigint is incompatible with uniqueidentifier

like image 955
Visions Avatar asked Jun 20 '12 08:06

Visions


People also ask

What is data type Uniqueidentifier?

Uniqueidentifier is a Microsoft SQL Server data type that is used to store Globally Unique Identifiers (GUIDs). It can store 16 bytes of data. The Developer tool treats the Uniqueidentifier data type as String.

How do I create a Uniqueidentifier in SQL?

Examples. The following example converts a uniqueidentifier value to a char data type. DECLARE @myid uniqueidentifier = NEWID(); SELECT CONVERT(CHAR(255), @myid) AS 'char'; The following example demonstrates the truncation of data when the value is too long for the data type being converted to.

How insert values into Uniqueidentifier column in SQL?

Use the newid() function to populate the global ID or GUID column when you insert a record to the table. Note that you could use the Next_GlobalID stored procedure to get the next ID value.

Is Uniqueidentifier auto generated?

Solution. Yes, there are a number of ways you can auto-generate key values for your tables. The most common ways are via the use of the IDENTITY column property or by specifying a uniqueidentifier (GUID) data type along with defaulting with either the NEWID() or NEWSEQUENTIALID() function.


1 Answers

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.

like image 169
Ram Singh Avatar answered Nov 06 '22 06:11

Ram Singh