SQL Server: having GUIDs for every row.

Hi, I'd like to each row in the table have GUID column in addition to IDENTITY column. What are different ways to accomplish this task?
Thank you in advance.
quasar_eeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
I'll bite.  Why?   An identity column already uniquely identifies a row.

Either way, give this a whirl..
ALTER TABLE YourTable
ADD GUIDColumn uniqueidentifier

UPDATE YourTable
SET GUIDColumn = NEWID()

Open in new window

0
Brian CroweDatabase AdministratorCommented:
Add the GUID column and default it to newid()

ALTER TABLE myTable
	GUID uniqueidentifier NULL
GO
ALTER TABLE myTable CONSTRAINT
	DF_Test_GUID DEFAULT newid() FOR GUID

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dwkorCommented:
Well, in either case do not create indexes on GUID. Those indexes become heavily fragmented and slows down batch operations. If you absolutely need to query by GUID consider to create and index calculated column with CHECKSUM(guid). This would improve performance of the index.

Something like that - you can use such technique with GUIDs too. http://aboutsqlserver.com/2010/12/05/sunday-t-sql-tip-equality-predicate-on-large-varchar-column/ In fact, I believe you do not even need to make calculated column persisted. You can still index regardless of it.
0
Anthony PerkinsCommented:
I believe you do not even need to make calculated column persisted. You can still index regardless of it.
I suspect you will find that you will get an error on a computed column that is not persisted.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.