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

sql Server 2005中,我试图在索引视图中使用用户定义函数,该视图将用于全文索引.我已经能够让UDF使用存储过程和有问题的视图.但是,当我尝试在视图上创建索引时,我收到以下错误

无法在视图“DevDatabase.dbo.View_PersonSearch”上创建索引,因为视图引用的函数“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在其参数方面不具有确定性 – 它使用select,这意味着结果可能随着数据的变化而变化 – 因此使用它的任何视图都不能被索引.

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...