How to Generate New GUID Using SQL Server


How to Generate New GUID Using SQL Server? In this article I'm going to explain how to generate Random unique identifier in using sql query in sql server. By using NEWID() function you can generate a new Guid (uniqueidentifier) in sql server.

How to Generate New Guid Using SQL Server?

In this article I'm going to explain how to generate Random unique identifier in using sql query in sql server.
By using NEWID() function you can generate a new Guid (uniqueidentifier) in sql server.

Example:-


SELECT NEWID() as Id


Output Example:-

-- This will return a new random uniqueidentifier example.
E37BCA1F-E9F6-435A-A9FE-4A99A67FB201

To select this Guid in in a variable


DECLARE @UniqueID uniqueidentifier
SET @UniqueID = NEWID()
select @UniqueID as ID

Output Example:-
C0B73F65-FBAE-4EB0-9E57-D0B2623A40C7


You can directly use this with INSERT statement to insert new row in table.


INSERT INTO Tablename(ID, Name)
VALUES
(NEWID(), 'Renganathan')


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: