views:

64

answers:

3
+1  Q: 

Generating GUID

I am thinking to use a GUID in my .net app which uses SQL Server. Should I be writing a stored procedure which generates the GUID on each record entered or should I be directly generating it from the application.

Reasons for asking the question (If am wrong correct me in this):

I (as/pre)sume:

When generating the GUID from the database, you can assume that the DB remembers the previous generated GUID where as the application remembering it is difficult.

+1  A: 

No, your assumption is wrong: the database won't be remembering anything - so there's no benefit from that point of view.

If you're using the GUID as your primary key / clustering key in SQL Server, which is a bad idea to begin with (see here, here or here why that's the case), you should at least use the newsequentialid() function as default constraint on that column.

CREATE TABLE YourTable(ColumnA uniqueidentifier DEFAULT NEWSEQUENTIALID()) 

That way, the database would generate pseudo-sequential GUID's for your PK and thus would make the negative effects of using a GUID as PK/CK at least bearable....

If you're not using the GUID as your primary key, then I don't see any benefit in creating that GUID on the server, really.

marc_s
@marc_s - I can think of some valid bussiness cases where using a (sequential) GUID would be a good choice. AMOF, if we knew a few years ago what we know now, we would most likely have implemented them and saved us much trouble (and money) trying to exchange data back and forth with our head branch.
Lieven
@lieven: seems you're talking about replication - that's probably the only valid case where you really need them. Make them your PK, but if ever possible, use something else as your clustering key!
marc_s
+1  A: 

SQL Server has the creation of GUID's built in. There is no need to write a separate stored procedure for this.

You can use

The key difference between both procedures would be that the sequential GUID should be used if it is for a primary clustered key.

I'm not sure why you would want the database engine to remember the previous generated GUID.

Lieven
A: 

My preference is to create GUID in the application not the db.

Tim Murphy