计算列无法持久化 [英] Computed Column cannot be Persisted

查看:32
本文介绍了计算列无法持久化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义函数,我正在尝试使用该函数创建一个持久化列.

I have a custom function, and I am trying to created a persisted column using this function.

它给了我以下错误.

表 'SomeTable' 中的计算列 'FormattedSSN' 无法持久化,因为该列是不确定的.

Computed column 'FormattedSSN' in table 'SomeTable' cannot be persisted because the column is non-deterministic.

功能如下:

ALTER FUNCTION [dbo].[FormatSSN]()
RETURNS VARCHAR(11)
AS
BEGIN
    return '';
END

这是使用该函数添加列的查询:

Here is the query to add the column using the function:

ALTER TABLE SomeTable
ADD FormattedSSN as dbo.FormatSSN() PERSISTED

请建议是否有任何出路.谢谢.

Please suggest if there is any way out. Thanks.

推荐答案

像这样将 WITH SCHEMABINDING 添加到函数中:

Add WITH SCHEMABINDING to the function like this:

ALTER FUNCTION [dbo].[FormatSSN]
(
@SSN    VARCHAR(9)
)
RETURNS CHAR(11)
WITH SCHEMABINDING
AS
BEGIN
  your stuff here
END

然后运行这个来验证:

IF OBJECTPROPERTY (OBJECT_ID(N'[dbo].[FormatSSN]'),'IsDeterministic') = 1
   PRINT 'Function is detrministic.'
ELSE IF OBJECTPROPERTY (OBJECT_ID(N'[dbo].[FormatSSN]'),'IsDeterministic') = 0
   PRINT 'Function is NOT detrministic'
GO

在这里工作.

这篇关于计算列无法持久化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆