25

I'm about to migrate my database from using old membership to the one included in mvc4, which uses int instead of guid.

When changing the type I get following error:

Conversion from int to uniqueidentifier is not supported on the connected database server.

How can I change UserId to int via SQL Server Management Studio?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
I'm busy coding
  • 1,648
  • 4
  • 13
  • 13
  • 1
    Those datatypes are not compatible, you'll need to convert them using a calculation that works for you – Lamak Feb 25 '13 at 14:27

4 Answers4

77

First Change Data Type to any other supported Type like varbinary or anything. then you can convert from that to what you want.

Muhamed Shafeeq
  • 1,194
  • 1
  • 9
  • 15
10

You have to add a new column ( ALTER TABLE ADD [NewId] INTEGER ) then run the following to populate the new id column :

WITH Cte
AS
(
    SELECT *
    , ROW_NUMBER() OVER(ORDER BY [Your GUID Column Here] DESC) AS RowNumber
    FROM YourTable
)
UPDATE Cte
SET [NewId]= RowNumber
GO

There you have a new ID column that you can use a clustered primary key

jazzytomato
  • 6,994
  • 2
  • 31
  • 44
2

With reference to this post:

Deleting the current column and adding a new column with the required data type should resolve your issue.

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
Stefan27
  • 845
  • 8
  • 19
1

as we know; the unique identifier variable is 32 bytes; int is 8 bytes and bigint is 16 bytes.

we make convert ,first byte and after uniqueidentifier.

Simple code;

Select CAST( CAST(cast(0 as bigint) AS varbinary)+CAST(cast(88 as bigint) AS varbinary) as uniqueidentifier)
A.Kosecik
  • 53
  • 3