151

I need to create an Id as Guid in SQL(no identity) How I can do this? I defined the Id as uniqueidentifier but what is save in Db is 00000000-0000-0000-0000-000000000000

Jim G.
  • 15,141
  • 22
  • 103
  • 166
Alma
  • 3,780
  • 11
  • 42
  • 78

4 Answers4

248

Use NEWID() to generate a Guid in SQL Server like this:

SELECT NEWID()
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Amazigh.Ca
  • 2,753
  • 2
  • 11
  • 8
11

Create a variable of uniqueidentifier data type. Type the below code in SSMS and execute.

 DECLARE @guid uniqueidentifier = NEWID();
 SELECT @guid as 'GUID';

Here we created a variable named guid of data type uniqueidentifier.

Efrat Ifergan
  • 184
  • 1
  • 5
7

A aspnet role created manually

DECLARE @ID UNIQUEIDENTIFIER
SET @ID = NEWID()

DECLARE @STAMP UNIQUEIDENTIFIER
SET @STAMP = NEWID()

INSERT INTO [dbo].[AspNetRoles]
           ([Id]
           ,[Name]
           ,[NormalizedName]
           ,[ConcurrencyStamp])
     VALUES
           (@ID
           ,'Admin'
           ,'ADMIN'
           ,@STAMP)
Diego Venâncio
  • 5,698
  • 2
  • 49
  • 68
0

To generate a GUID in SQL Server, use the NEWID() function. It produces a globally unique alphanumeric value of 128 bits, ideal for primary keys or unique identifiers in databases.

SELECT NEWID() AS GeneratedGUID;