Geo*_*voy 13 sql-server calculated-columns
我在SQL Server 2005中的大表中有一个持久的计算列.
我想将它转换为常规列,保持当前值.
我是否必须重新创建列并在事务中更新整个表,或者是否可以只更改计算列规范,以及如何执行此操作?
Mit*_*eat 13
-- Create a new Column (unpersisted):
ALTER TABLE MyTable
ADD newColumn DatatypeOfPersistedColumn
GO
UPDATE myTable
SET newColumn = PersistedColumn
GO
-- Delete the persisted column
ALTER TABLE MyTable
DROP COLUMN PersistedColumn
GO
-- Rename new column to old name
EXEC sp_rename 'MyTable.newColumn', 'PersistedColumn', 'COLUMN'
GO
Run Code Online (Sandbox Code Playgroud)
-- Create a new Column (unpersisted):
ALTER TABLE MyTable
ADD newColumn DatatypeOfPersistedColumn
UPDATE myTable
SET newColumn = PersistedColumn
-- Delete the persisted column
ALTER TABLE MyTable
DROP COLUMN PersistedColumn
-- Rename the new column to the old name
EXEC sp_rename 'MyTable.newColumn', 'PersistedColumn', 'COLUMN'
Run Code Online (Sandbox Code Playgroud)