Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everybody,

I have a SqlCe 4.0 DB and I want to maintain the continuity of indexes in the DB even if a row is deleted.

I have a column with an auto-increment identity.

I.E.
I have three rows:

ID        | Name     |
0           John
1           Jimmy
2           Natalie


If I want to delete Jimmy, the other rows has to become:

ID        | Name     |
0           John
1           Natalie


and NOT

ID        | Name     |
0           John
2           Natalie


as it will be.

Is there a query compatible with SqlCe 4.0 which accomplishes this result?

Thanks!!

Jymmy097

PS: whenever I have to do this, I manually delete Id and, then, regen it by using a DB designer. This, I suppose, should be the equivalent of an ALTER TABLE, but the designer tells me that ALTER TABLE is not compatible with SqlCe...
Posted
Updated 11-Jun-14 11:20am
v2
Comments
Jörgen Andersson 11-Jun-14 17:23pm    
Why on earth do you want to do that, one of the points with an identity is that it's immutable. One and the same number always refer to the same entity.
Indexes doesn't bother about the sequence at all. So what's the point?

1 solution

That is not a worthwhile goal. You should never care what the IDs are and you should never change them. What are you really trying to accomplish?


If you simply want to have a user-friendly ordering for display you could do something like the following:

CREATE TABLE y ( RN INTEGER IDENTITY , ID INTEGER , Name NText )
INSERT INTO y ( ID , Name ) SELECT ID , Name FROM x ORDER BY ID
SELECT * FROM y
DROP TABLE y


In full Sql Server I'd use the ROW_NUMBER function, but CE doesn't have it. Nor does it have temporary tables. Not even SELECT/INTO.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900