all groups > sql server programming > july 2004 >
You're in the

sql server programming

group:

uniqueidentifier column


uniqueidentifier column Fernando Chilvarguer
7/13/2004 9:00:18 PM
sql server programming: I'm using a uniqueidentifier column as the "identity" primary key column for
a table.
How can I retrieve the value of that column after an insert (using NEWID())?
I tried something like:
"INSERT INTO TABLE........... SELECT @@Identity"
The @@Identity does not work in this case. It returns NULL

Thanks,
Fernando Chilvarguer

uniqueidentifier column Knick
7/13/2004 9:45:33 PM
I don't think that you can use identity on
UniqueIdentifier. It must use newid() only.
[quoted text, click to view]
Re: uniqueidentifier column Joe Celko
7/13/2004 10:33:04 PM
[quoted text, click to view]
column for a table. <<

That makes no sense in relational terms. Please quit writing such
low-level, non-relational, non-portable, proprietary code before you put
your employer out of business.

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
Re: uniqueidentifier column Fernando Chilvarguer
7/13/2004 11:45:10 PM
Thanks for all the answers. It fixed my problem.

Now another question:

Which one would be the "best practice" way of doing it:

1. Using the indentity field with @@Identity or
2. Using a uniqueidentifier field and the NEWID() function?

Or would it be just a matter of personal preference?

Thanks again,
Fernando


[quoted text, click to view]

Re: uniqueidentifier column Fernando Chilvarguer
7/13/2004 11:51:17 PM
It would be a lot more helpful to suggest a better way of doing it instead
of just criticizing.
Your reply did not add anything useful to this thread.

[quoted text, click to view]

Re: uniqueidentifier column Dan Guzman
7/14/2004 8:41:49 AM
[quoted text, click to view]

I'd like to add:

3. Use the real key instead of a surrogate key.

Each technique has both advantages and disadvantages. #3 is often best if
your database is properly normalized. However, you might choose to
introduce a surrogate key for reasons such as avoiding a large composite key
and/or performance.

IDENTITY is more compact than uniqueidentifier and values are assigned
sequentially. This reduces storage requirements and provides more efficient
index maintenance during inserts.

Uniqueidentifier values are assigned randomly (in Windows 2000 and above).
These can be assigned on the client side and thereby eliminate a round-trip
to retrieve the assigned value.

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]

Re: uniqueidentifier column Joe Celko
7/14/2004 10:05:14 AM
[quoted text, click to view]
instead of just criticizing. Your reply did not add anything useful to
this thread. <<

If this was a woodworking group and someone asked what is the best kind
of rock for smashing screws into fine furniture, would you reply
"Granite! Big chunks of granite!" or would you reply "Do you know about
a screwdriver?" instead?

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
Re: uniqueidentifier column Hari Prasad
7/14/2004 10:07:43 AM
Hi,

Unique identifier is not like an identity property. It is a 16 bit
hexadecimal number. There is no functions like @@identity to get the last
inserted value. The only thik before you insert the unque identifier just
store it in a vatiable.


DECLARE @myid uniqueidentifier
SET @myid = NEWID()
insert into table(myid) values(@myid)
PRINT 'Value of @myid is: '+ CONVERT(varchar(255), @myid)


--
Thanks
Hari
MCDBA
[quoted text, click to view]

Re: uniqueidentifier column Greg Linwood
7/14/2004 2:48:22 PM
Hi Fernando

You have to capture the value BEFORE inserting it, not after. It's quite the
opposite of identity in this respect.

eg:

declare @id uniqueidentifier
select @id = newid()
insert into mytable (udcolumn) values (@id)
....

Knowing the value before it's inserted is one of the key advantages of using
GUIDs over IDENTITIES.

HTH

Regards,
Greg Linwood
SQL Server MVP

[quoted text, click to view]

Re: uniqueidentifier column Hari Prasad
7/14/2004 2:49:35 PM
Thank you Greg for pointing out. Typo error.

--
Thanks
Hari
MCDBA

[quoted text, click to view]

Re: uniqueidentifier column Hari Prasad
7/14/2004 2:56:33 PM
Hi,

The major advantage of using GUIDs is that they are unique across all space
and time. This comes in handy if you're consolidating records from multiple
SQL Servers into one table, as in a data warehousing situation.


Have a look into this article which details your query.

http://www.sqlteam.com/item.asp?ItemID=283

--
Thanks
Hari
MCDBA
[quoted text, click to view]

Re: uniqueidentifier column Greg Linwood
7/14/2004 4:46:43 PM
Hi Hari.

GUIDs use 16 bytes for storage, not 16 bits.. (^:

Regards,
Greg Linwood
SQL Server MVP

[quoted text, click to view]

Re: uniqueidentifier column Fernando Chilvarguer
7/14/2004 8:56:02 PM
That's exactly my point.
You did not offer the "screwdriver" option. All you said was "Granite? Are
you stupid?"

[quoted text, click to view]

Re: uniqueidentifier column Greg Linwood
7/15/2004 12:05:02 AM
Another consideration I'd add is that using @@identity limits (close to
eliminates) your ability to use SQL2K partitioned views for performance in
the future.

Regards,
Greg Linwood
SQL Server MVP

[quoted text, click to view]

Re: uniqueidentifier column Greg Linwood
7/15/2004 12:05:31 AM
I agree!! (^:

Regards,
Greg Linwood
SQL Server MVP

[quoted text, click to view]

AddThis Social Bookmark Button