views:

55

answers:

2

Hy,

I'm new with SQL Compact Edition and I am having problems renaming a column. I know that you can rename a table using sp_rename and this doesn't work with columns.

I've searched for an alternative, but didn't find one.

Can I delete a column and then add a new one after a specific column? If I delete the column and add it after the a specified one the data would be lost right?

Is this another issue of SQLCE that once you have created the table it can't be properly modified?

Thanks in advance

A: 
Kamyar
Are you sure that it works with Compact Edition as well? I've tried it before posting this question and I've tried it using your example and it doesn't work. Here's what i got: The specified argument value for the procedure is not valid. [ Argument # = 3,Name of procedure(if known) = sp_rename ]
lucian
You're right. sorry. seems like sp_rename is not available in SQLCE. updating my answer...
Kamyar
+2  A: 

It does indeed seem that SQL CE wont allow changing column names.

You're on the right track with creating a new column and deleting the old.

If you just add a column and delete the old you will loose the data so you need to issue an update statement to shift the data from the old to the new.

Something along the lines of

alter Table [dbo].[yourTable] add [newColumn]

update yourTable set newColumn = oldColumn

alter Table [dbo].[yourTable] drop column [oldColumn]

Should create your new column, duplicate the data from old to new and then remove the old column.

Hope it helps!

Robb
It's possible that this could work, but the new added column will be the last column in the table and I don't want that. There is after so you can add after a specified column but it doesn't work in CE. It can't find the keyword after. This is my query: "ALTER TABLE Inverters ADD COLUMN NewC int after Product", this is the error: "There was an error parsing the query. [ Token line number = 2,Token line offset = 21,Token in error = after ]".
lucian
Alter Table in CE seems to be missing that functionality from looking here http://msdn.microsoft.com/en-us/library/ms174123.aspx Lacking this I'm not sure how you could preserve your current order short of recreating the table. Personally I wouldn't be overly worried about that as column ordering would be specified by my select statement anyway.
Robb