Skip to content
Advertisement

How to generate a new Guid in stored procedure?

I currently have a stored procedure in which I want to insert new rows into a table.

insert into cars
(id, Make, Model)
values('A new Guid', "Ford", "Mustang")

So the primary key ‘id’ is a Guid. I know how to create a new Guid in C# code but within the stored procedure I’m unsure how to generate the new Guids for the primary key values.

Advertisement

Answer

With SQL Server you can use the function NEWID. You’re using C# so I assume that you’re using SQL Server. I’m sure other database system have similar functions.

select NEWID()

If you’re using Oracle then you can use the SYS_GUID() function. Check out the answer to this question: Generate a GUID in Oracle

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement