Auto-increment [id] field in a table in an SQL-Server database
0
votes
1
answer
313
views
I have different tables, containing an
Thanks in advance
id
field. That field is typically defined as id (PK, int, not null)
.
Normally, when adding an entry to a table, I add the value of the id
field myself, but I'm wondering if there's no automatic way to do that:
The following is not working: (there already is an entry with id
being 1)
INSERT [dbo].[Settings] ([GroupId], [Value], [Name], [Id], [Description], [Admin])
VALUES ( 1, N'1', N'BLABLA', 1, N'meer blabla', 0)
The following is also not working (the id
field not being filled in):
INSERT [dbo].[Settings] ([GroupId], [Value], [Name], [Description], [Admin])
VALUES ( 1, N'1', N'BLABLA', N'meer blabla', 0)
I know about the possibility to add a kind of IDENTITY
table, which might handle this, but I'm very reluctant to add another table to the customer's database.
Does anybody know a way to get this done? If possible, I would like to have two possibilities:
- Finding the first possible id (using holes in the existing ids, if any): imagine that I have the following list of existing ids: (1, 2, 3, 4, 11, 12, 13, 14). Then I want 5 to be the next one.
- Finding the highest id plus one: imagine that I have the following list of existing ids: (1, 2, 3, 4, 11, 12, 13, 14). Then I want 15 to be the next one.
Does anybody know how to get this done in a one-liner SQL command, without altering the customer's database?
For your information, I'm using Telerik OpenAccess for creating and accessing the database, and one of the items I can see is this:
configuration.HasProperty(o => o.Id).IsIdentity();
However, for that table, I don't see that in SQL-Server:

Asked by Dominique
(609 rep)
Oct 26, 2023, 11:57 AM
Last activity: Nov 16, 2023, 04:56 PM
Last activity: Nov 16, 2023, 04:56 PM