tags:

views:

164

answers:

3

Is it possible to auto-generate a GUID into an Insert statement?

Also, what type of field should I use to store this GUID?

Thanks!

+7  A: 

You can use the SYS_GUID() function to generate a GUID in your insert statement:

insert into mytable (guid_col, data) values (sys_guid(), 'xxx');

The preferred datatype for storing GUIDs is RAW(16).

Tony Andrews
Weird, the sys_guid() is always giving me the same GUID.. Do I need to give a seed to the function or?
Acibi
Are you sure they are exactly the same? It does tend to return very similar (but different) values - e.g. when I just tried I got 88FDC68C75DEF955E040449808B55601 and 88FDC68C75DFF955E040449808B55601, which differ only at the 12th character!
Tony Andrews
Oh, yeah!Thanks a lot, I was reading them too fast ;)
Acibi
A: 

Check this link, it may be useful to you

http://feuerthoughts.blogspot.com/2006/02/watch-out-for-sequential-oracle-guids.html

Bharat
Yeah, that was the first or second result after googling for "oracle create guid". Makes you kinda feel like some obscene Google proxy. o_O
Robert Giesecke
@Robert Giesecke :It think that posting the links (although they were in google 1st page) is not obscene until they are useful to the questioner. Please post sensible comments
Bharat
Holy c***! Don't know what I wanted to write, it certainly wasn't "obscene". My colleague was talking to me about something creepy while I typed, so hope I can blame it on a freudian slip...
Robert Giesecke
+1  A: 

It is not clear what you mean by auto-generate a guid into an insert statement but at a guess, I think you are trying to do something like the following:

INSERT INTO MY_TAB (ID, NAME) VALUES (SYS_GUID(), 'Adams');
INSERT INTO MY_TAB (ID, NAME) VALUES (SYS_GUID(), 'Baker');

In that case I believe the ID column should be declared as RAW(16);

I am doing this off the top of my head. I don't have an Oracle instance handy to test against, but I think that is what you want.

Kenneth Baltrinic