我有一个主要的详细信息表和一个使用Sum(Qty * Fee)的函数,并希望在主表中用作计算列并坚持它,但是这个错误引发了,这里是我的表和函数:
CREATE TABLE [dbo].[Table_1]
(
[Id] [BIGINT] IDENTITY(1,1) NOT NULL,
[Qty] [INT] NULL,
[Fee] [MONEY] NULL,
[T2Id] [BIGINT] NULL,
CONSTRAINT [PK_Table_1]
PRIMARY KEY CLUSTERED ([Id] ASC)
)
CREATE TABLE [dbo].[Table_2]
(
[Id] [BIGINT] NOT NULL,
[Name] [NVARCHAR](255) NULL,
CONSTRAINT [PK_Table_2]
PRIMARY KEY CLUSTERED ([Id] ASC)
)
ALTER FUNCTION [dbo].[Test](@Id INT)
RETURNS MONEY
WITH SCHEMABINDING
AS
BEGIN
DECLARE @Total MONEY
SELECT @Total = CAST(SUM(Qty * Fee) AS MONEY)
FROM dbo.Table_1
WHERE T2Id = @Id
RETURN ISNULL(@Total, 0)
END
这是我的T-SQL添加SumRow
以及产生的错误:
ALTER TABLE dbo.Table_2
ADD SumRow AS dbo.test(Id) PERSISTED
错误:
Msg 4934,Level 16,State 3,Line 1
表'Table_2'中的计算列'SumRow'无法保留,因为该列可以进行用户或系统数据访问。
此检查也是如此:
SELECT OBJECTPROPERTY (OBJECT_ID(N'[dbo].[test]'),'IsDeterministic')