gpt4 book ai didi

sql-server - 无法使用 SQL Server 中的用户定义函数在 View 上创建索引

转载 作者:行者123 更新时间:2023-12-02 11:03:35 26 4
gpt4 key购买 nike

在 SQL Server 2005 中,我尝试在索引 View 中使用用户定义的函数,该函数将在全文索引中使用。我已经能够让 UDF 与存储过程和相关 View 一起使用。但是,当我尝试在 View 上创建索引时,出现以下错误...

无法在 View “DevDatabase.dbo.View_PersonSearch”上创建索引,因为 View 引用的函数“dbo.GetCurrentImage”执行用户或系统数据访问。

我被这个难住了。下面是我正在尝试做的事情的一个例子。我是否遗漏了什么或者这是否可能?

用户定义函数

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER FUNCTION [dbo].[GetCurrentImage](@Person_ID int)
RETURNS int
WITH SCHEMABINDING
AS
BEGIN

-- Declare the return variable here
DECLARE @Img_ID int

SET @Img_ID = (**sql that selects image** )

RETURN @Img_ID

END
GO

查看并创建索引

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER VIEW [dbo].[View_PersonSearch]
WITH SCHEMABINDING
AS
SELECT Person_ID,
(**Select fields to search on**) AS SearchArea,
dbo.GetCurrentImage(Person_ID) AS FK_Img_ID
FROM dbo.Person
GO

CREATE UNIQUE CLUSTERED INDEX Index_Person_ID ON [View_PersonSearch](Person_ID)
GO

最佳答案

根据this page :

Any functions referenced in an indexed view must be deterministic; deterministic functions return the same value each time they’re invoked with the same arguments.

GetCurrentImage 的参数不是确定性的 - 它使用选择,这意味着结果可能会随着数据的变化而变化 - 因此任何使用它的 View 都无法被索引。

关于sql-server - 无法使用 SQL Server 中的用户定义函数在 View 上创建索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/631715/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com