我正在使用 SQL Server 2008。是否可以更改计算列,而无需实际删除该列,然后再次添加它(我可以开始工作)?例如,我有这张表:
CREATE TABLE [dbo].[Prices](
[Price] [numeric](8,3) NOT NULL,
[AdjPrice] AS [Price] / [AdjFactor],
[AdjFactor] [numeric](8,3) NOT NULL)
后来意识到我有潜在的被零除错误,我想更改 [Adjprice] 列来处理此问题,但如果我只是删除该列并再次添加它,我会丢失列顺序。
我想做这样的事情:
ALTER TABLE dbo.[Prices]
ALTER COLUMN [AdjPrice] AS (CASE WHEN [AdjFactor] = 0 THEN 0 ELSE [Price] / [AdjFactor] END)
但这并不正确。如果这是可能的,或者有其他解决方案,我将不胜感激。
最佳答案
不幸的是,如果不先删除该列,您就无法执行此操作。
<强> From MSDN :
ALTER COLUMN
Specifies that the named column is to be changed or altered. ALTER COLUMN is not allowed if the compatibility level is 65 or lower. For more information, see sp_dbcmptlevel (Transact-SQL).
The modified column cannot be any one of the following:
- A computed column or used in a computed column.
关于SQL - 如何更改计算列上的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2274486/