I have different tables, containing an 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:
Thanks in advance
ID
field an auto-increment without adding another table, but do you want to do it on a table that already has data in it or an empty table?IDENTITY
property. Unfortunately youo can't add it to an existing column, you need to drop and recreate the column or the whole table.id
field, and I'm creating SQLINSERT INTO
queries in order to add more data to that table (there already are data present in that table).