更改要计算的列 SQL SERVER

Alter a column to be calculated SQL SERVER

我有一个 table 类似于这个:

CREATE TABLE [dbo].[test](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [action] [varchar](50),
    [id_source] [int] NULL,
    [id_dest] [int] NULL,
    [name] [varchar](255)
)

我正在尝试转换要计算的 name 列。我试过类似的东西但没有成功

ALTER TABLE [dbo].[test] 
ALTER COLUMN [name] AS ([dbo].[f_get_name]([id_source],[id_dest],[action]))

table目前为空。 是否可以使用 ALTER 命令实现此目的? 谢谢

你不能改变它。

ALTER COLUMN

Specifies that the named column is to be changed or altered.

The modified column cannot be any one of the following:

A column with a timestamp data type.

The ROWGUIDCOL for the table.

A computed column or used in a computed column.

您需要删除并重新创建:

ALTER TABLE [dbo].[test]
DROP COLUMN [name];

ALTER TABLE [dbo].[test]
ADD [name] AS ([dbo].[f_get_name]([id_source],[id_dest],[action]));

Is it possible to achieve this with an ALTER command?

不,没有办法通过改变 table 来实现它。 ALTER table 命令将限制您这样做,因为您的列涉及计算。

解法:

您需要像这样删除该列并重新创建它:

alter table [dbo].[test] 
drop column [name];

alter table [dbo].[test] 
add [name] as ([dbo].[f_get_name]([id_source],[id_dest],[action]));