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 323 times
3 Answers
4
You could use a Guid, and set default value for the column to newid()
.
![](../../users/profiles/591296.webp)
Nils Magne Lunde
- 1,721
- 1
- 13
- 20
-
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:
![](../../users/profiles/-1.webp)
Community
- 1
- 1
![](../../users/profiles/326370.webp)
Ilya Kogan
- 20,368
- 15
- 78
- 134
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.
![](../../users/profiles/15393.webp)
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