Index on computed column

First, I would like to say that the "XML Best Practices for Microsoft SQL Server 2005" article is a must read for everyone interacting with this forum. It's a GREAT article!

There is a section on "Property Promotion", and everything is working fine until I get to the point where I am attempting to create a unique index on my promoted column. I do everything the same as the example below, and I get the following error when attempting to create the index:

Column 'ad_guid' in table 'employees' cannot be used in an index or statistics or as a partition key because it is non-deterministic.

The only difference between my case, and the one below, is that the field I am promoting is typed as a uniqueidentifier - rather than varchar.

Any ideas/suggestions?

Thanks,

Scott

-

http://msdn.microsoft.com/xml/default.aspx?pull=/library/en-us/dnsql90/html/sql25xmlbp.asp

CREATE FUNCTION udf_get_book_ISBN (@xData xml) RETURNS varchar(20) BEGIN DECLARE @ISBN varchar(20) SELECT @ISBN = @xData.value('/book[1]/@ISBN', 'varchar(20)') RETURN @ISBN END

Add a computed columnto the table for ISBN:

ALTER TABLE docs ADD ISBN AS dbo.udf_get_book_ISBN(xCol)

The computed column can be indexed in the usual way.

[1852 byte] By [TopLevelXquery] at [2007-12-19]
# 1

Nevermind...Just found the answer in one of Michael's precious replys:

Need to add "with schemabinding" to the function as follows:

CREATE FUNCTION udf_get_book_ISBN (@xData xml)
RETURNS varchar(20)

with schemabinding
BEGIN
DECLARE @ISBN varchar(20)
SELECT @ISBN = @xData.value('/book[1]/@ISBN', 'varchar(20)')
RETURN @ISBN
END

After that, everything works fine.

Scott

TopLevelXquery at 2007-9-8 > top of Msdn Tech,SQL Server,SQL Server XML...

SQL Server

Site Classified