tsql - SQL Server unique GUID -


i understand in sql server guids unique, , likelihood of collision remote, yet @ same time must win lottery feel makes sense prepare possibility.

which faster/better practice

using technique assign new guid directly inserting row , checking error (@@error <> 0) , repeating until don't error [which suppose in theory @ worst once...]

or using approach this

declare @myguid uniqueidentifier select @myguid = newid() if exists(select * tablename userid=@myguid) 

and looping on till find 1 not in use.

i 2nd approach because can have guid use later on in stored procedure i'm leaning towards one.

to answer question , not debate merit of question/perceived problem.

the first implementation 1 want use 2 reasons:

  • running check exists before doing insert every single record dealing in end result in more resources being dedicated extremely unlikely happen. (also database ensure constraint on column if collide data wont committed)
  • if have error on first one, can take little time , handle error returned.

you can combine 2 , declare new uniqueidentifier insert table (if works continue using it, else retry new 1 , continue using it).

generally want program situation first , handle exceptions last.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -