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.
Asked
Active
Viewed 334 times
3 Answers
4
You could use a Guid, and set default value for the column to newid()
.

Nils Magne Lunde
- 1,794
- 1
- 13
- 21
-
How can i use it in back end, having any idea? – NoviceToDotNet Apr 15 '11 at 11:43
-
If you are inserting data into a table from your application, you can omit the PK because it will be created automatically. – Nils Magne Lunde Apr 16 '11 at 19:18
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:

Community
- 1
- 1

Ilya Kogan
- 21,995
- 15
- 85
- 141
1
You've got at least 3 options:
- Make it an IDENTITY column, and use
SET IDENTITY_INSERT
(see link) to allow you to insert values when you have one. - Use a function to set the default value.
- 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