views:

265

answers:

1

Hello

I am trying to insert a value in a one IDENTITY column Table in SQL Server CE 3.5. I Tried the following:

INSERT Target DEFAULT VALUES
INSERT Target (ID) VALUES (DEFAULT)
INSERT Target (ID) VALUES ()

But none of them worked. This is the SQL command I used to create the table (Using SQL Server Management Studio):

CREATE TABLE Target(
ID int NOT NULL IDENTITY (1, 1) PRIMARY KEY
);

Microsoft help site (http://msdn.microsoft.com/en-us/library/ms174633%28SQL.90%29.aspx) mentions that DEFAULT values are not valid for identity columns however they do not mention any alternative.

They mention something about uniqueidentifier and ROWGUID but I have not been able to make it work.

I would appreciate any pointers on how to solve this problem or links to documentation about valid sql commands for sql server CE.

Thank you

+3  A: 

Using Default Values works for identity columns on the standard version of SQL. I can't see any reason why it wouldn't work on CE.

In your case you would do something like this:

Insert Into Target
Default Values

Edit:

This issue looks like it is specific to SQL CE.

The only other thing I could suggest would be to add another column to your table, such as DateInserted.

Insert Into Target (DateInserted)
Values (getdate())

This should then insert a new row thus generating a new ID.

If you can change your table structure then you could us a UniqueIdentifier instead.

Create Table Target
(
IDColumn uniqueidentifier not null
)

Insert Into Target
Values (newId())
Barry
Hello Barry I tried that query, it did not work it says there was an error in the Default token.
Hei
Yes, definitely - `INSERT INTO Target DEFAULT VALUES` definitely works on SQL Server 2008 - maybe it's a problem specific to SQL Server CE ?
marc_s
@Barry Thank you for your answer the first solution would work however i was trying to avoid altering the table. The Second solution works as well but it inserts extremely long and non sequential ids
Hei
YOu know not to count on identities being sequnetial either right? Any transactions that are rolled back will use up an identity value, so there will be gaps (although they will always be larger than the previous value).
HLGEM
@HLGEM - that is a good point re: identities.@Hei - Is there a particular reason why the length of the ID matters and whether it is sequential?
Barry
@Barry Well the id in target is referenced by two other tables primary keys, the point of the target table was using it as a sort of sequence. On the other hand is true i could use the ID generated as long as it is unique.
Hei
I think if you wanted to guarantee that a table holds sequential numbers you would have to generate it manually with maybe some sort of status to state whether the insert/update was successful. However, in doing this you would have to lock the table to ensure that two or more inserts won't try to create the next "sequential" number. This may cause deadlocks depending on the length of time of the insert takes.
Barry