views:

34

answers:

3

Hi, 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.

Can someone help me out please?

Thanks!

+3  A: 

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()
Adam Porad
Great, thanks for the answer!
Mr Cricket
+3  A: 

Try this:

SELECT NewId()
BradB
+2  A: 

You didn't ask about this in your question, but I think it's worth pointing out that using a GUID for a primary key is not always a good idea. While it's simple, it can affect performance when a GUID is used in an index. Have you considered using an Identity column that is an integer value instead?

Here are a couple of articles that might be helpful to read.

Adam Porad
Yes, I agree with you. However I'm working on a database which someone else developed. All the previous tables used a Guid as a primary key so I'm just trying to be consistent. Though for new tables maybe I should evaluate on a case by case basis. Thanks for the info though.
Mr Cricket