insert - SQL Server generate unique value -


sql server 10, .net, c#

this similar this question not same.

when user creates new event on event-scheduling web site, unique sort-of easy remember code generated, code can communicated (via phone, let's say) participants. generated code looks like:

johmonblue5
<abbriviated name><abbriviated week day><some uniquefying suffix><index>

yes, e-mail link, suppose that's not option.

note: chose use uniquefying suffix color, since think it's easier remember color, number. still use number in case of collision.

question: what's way implement (i have unique constraint on column already):

  1. i can generate index on app side, , use try { insert } catch { retry }. in case, how tell "duplicate column value" exception other exception? message text?
  2. use in mssql server increment trailing number until uniqueness reached.

i suggest not using catch handle duplicates exclusively. catch should used true errors , besides that, exception catching slow , expensive. better generate value , test see whether exists via query rather allowing exception thrown.

i implement generator in 1 place. don't split creating color on client , number in db. pick 1 side or other. on sql server side way of implementing type of feature use udf. perhaps can create table of keywords (colors in case) , randomly select , perform increment uniqueness.

i think don't have o colors exclusively. choose words simple , easy remember. dragon, friday, blue, house...


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -