我有一个带有PERSISTED
和一个基于函数(Test
)结果的索引计算字段(MyFunctionTest
)的表,例如:
CREATE TABLE [dbo].[TestTable] (
[Id] [int] IDENTITY(1,1) NOT NULL,
[Foo] [int] NOT NULL,
[Test] AS ([dbo].[MyFunctionTest]([Foo])) PERSISTED,
CONSTRAINT [PK_TestTable] PRIMARY KEY CLUSTERED
(
[Id] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
MyFunctionTest
是:
ALTER FUNCTION [dbo].[MyFunctionTest]
(
@foo int
)
RETURNS int
WITH SCHEMABINDING
AS
BEGIN
RETURN @foo * 2
END
如果我尝试更改功能,则SQL Server显示错误
不能更改'dbo.MyFunctionTest'它被对象'TestTable'引用。
改变功能的唯一方法似乎是创建一个新的改变表。
我还试图删除WITH SCHEMABINDING
答案 0 :(得分:1)
您可以删除列,更改功能并将列添加回去:
ALTER TABLE [dbo].[TestTable]
DROP COLUMN [Test];
GO
ALTER FUNCTION [dbo].[MyFunctionTest]
(
@foo int
)
RETURNS int
WITH SCHEMABINDING
AS
BEGIN
RETURN @foo * 2
END
GO
ALTER TABLE [dbo].[TestTable]
ADD [Test] AS ([dbo].[MyFunctionTest]([Foo])) PERSISTED
创建新功能还意味着您需要删除该列并将其添加回computed columns can't be altered之后。