0

how can i allocate a unique value to a column field in sql server, if no value is inserted? can i set some value on filed in sql server? i dont wan t to make it identity column, because some times records may be inserted from front end. But not always, in that case the column should automatically have a unique value which dont exist in the column already.

bluish
  • 26,356
  • 27
  • 122
  • 180
NoviceToDotNet
  • 10,387
  • 36
  • 112
  • 166

3 Answers3

4

You could use a Guid, and set default value for the column to newid().

Nils Magne Lunde
  • 1,794
  • 1
  • 13
  • 21
2

A GUID is a good option. Specifically, you can read about COMBs, which are a kind of GUIDs that perform better than ordinary GUIDs.

And here is another thread that you may find useful:

Performance value of COMB guids

Community
  • 1
  • 1
Ilya Kogan
  • 21,995
  • 15
  • 85
  • 141
1

You've got at least 3 options:

  1. Make it an IDENTITY column, and use SET IDENTITY_INSERT (see link) to allow you to insert values when you have one.
  2. Use a function to set the default value.
  3. Use an "AFTER INSERT" trigger to update the field if it is null.
RB.
  • 36,301
  • 12
  • 91
  • 131
  • Sorry - I screwed up the link. See the link I've added. Basically, it's a way of allowing you to insert a specified value into an identity column. – RB. Apr 04 '11 at 08:45
  • No, it don't work, i already have a column set identity column. I need other solution. – NoviceToDotNet Apr 15 '11 at 11:42