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
  • 23,093
  • 23
  • 110
  • 171
NoviceToDotNet
  • 9,183
  • 28
  • 103
  • 158

3 Answers3

4

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

Nils Magne Lunde
  • 1,721
  • 1
  • 13
  • 20
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
  • 20,368
  • 15
  • 78
  • 134
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.
  • 33,692
  • 12
  • 79
  • 121
  • 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