I am having problems renaming a column in SQL Server Compact Edition. I know that you can rename a table using sp_rename
, but this doesn't work with columns.
我在重命名SQL Server Compact Edition中的列时遇到问题。我知道您可以使用sp_rename重命名表,但这不适用于列。
I've searched for an alternative, but haven't found 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?
我可以删除列,然后在特定列之后添加新列吗?如果我删除列并在指定的列之后添加它,数据会丢失吗?
It seems that once you have created the table it can't be properly modified - is this another of the limitations of SQLCE?
看来,一旦你创建了表,它就无法正确修改 - 这是SQLCE的另一个限制吗?
3 个解决方案
#1
14
It does indeed seem that SQL CE wont allow changing column names.
确实,SQL CE似乎不允许更改列名。
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 lose 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!
希望能帮助到你!
#2
2
sp_rename works with columns too:
sp_rename也适用于列:
EXEC sp_rename
objname = '< Table Name.Old Column Name >',
@newname = '<New Column Name>',
@objtype = 'COLUMN'
Example:
例:
SP_RENAME 'MyTable.[MyOldColumnName]' , '[MyNewColumnName]', 'COLUMN'
UPDATE: Actually, The sp_rename procedure is not avialable in SQL CE! You can find the solution at http://www.bigresource.com/Tracker/Track-ms_sql-4Tvoiom3/
更新:实际上,sp_rename过程在SQL CE中是不可用的!您可以在http://www.bigresource.com/Tracker/Track-ms_sql-4Tvoiom3/找到解决方案。
#3
0
SDF Viewer has this function built in, you can also rename indexes, keys and relationships. Just right click on the name you want to change in the database treeview.
SDF Viewer内置了此功能,您还可以重命名索引,键和关系。只需在数据库树视图中右键单击要更改的名称即可。
#1
14
It does indeed seem that SQL CE wont allow changing column names.
确实,SQL CE似乎不允许更改列名。
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 lose 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!
希望能帮助到你!
#2
2
sp_rename works with columns too:
sp_rename也适用于列:
EXEC sp_rename
objname = '< Table Name.Old Column Name >',
@newname = '<New Column Name>',
@objtype = 'COLUMN'
Example:
例:
SP_RENAME 'MyTable.[MyOldColumnName]' , '[MyNewColumnName]', 'COLUMN'
UPDATE: Actually, The sp_rename procedure is not avialable in SQL CE! You can find the solution at http://www.bigresource.com/Tracker/Track-ms_sql-4Tvoiom3/
更新:实际上,sp_rename过程在SQL CE中是不可用的!您可以在http://www.bigresource.com/Tracker/Track-ms_sql-4Tvoiom3/找到解决方案。
#3
0
SDF Viewer has this function built in, you can also rename indexes, keys and relationships. Just right click on the name you want to change in the database treeview.
SDF Viewer内置了此功能,您还可以重命名索引,键和关系。只需在数据库树视图中右键单击要更改的名称即可。