SQL Server Alter Computed Column

旧巷老猫 提交于 2019-12-23 08:49:47

问题


Does anyone know of a way to alter a computed column without dropping the column in SQL Server. I want to stop using the column as a computed column and start storing data directly in the column, but would like to retain the current values.

Is this even possible?


回答1:


Not that I know of but here is something you can do

add another column to the table update that column with the values of the computed column then drop the computed column




回答2:


Ok, so let me see if I got this straight. You want to take a column that is currently computed and make it a plain-jane data column. Normally this would drop the column but you want to keep the data in the column.

  1. Make a new table with the primary key columns from your source table and the generated column.
  2. Copy the data from your source table into the new table.
  3. Change the column on your source table.
  4. Copy the data back.

No matter what you do I am pretty sure changing the column will drop it. This way is a bit more complex but not that bad and it saves your data.

[Edit: @SqlMenace's answer is much easier. :) Curse you Menace!! :)]




回答3:


If you need to maintain the name of the column (so as not to break client code), you will need to drop the column and add back a stored column with the same name. You can do this without downtime by making the changes (along the lines of SQLMenace's solution) in a single transaction. Here's some pseudo-code:

begin transaction
   drop computed colum X
   add stored column X
   populate column using the old formula
commit transaction


来源:https://stackoverflow.com/questions/44118/sql-server-alter-computed-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!