I have an XML column in a table; I want to “push” a specific value in this XML as a computed column and index it for faster searching. I have a function that accepts XML information and displays the element of interest, for example:
CREATE FUNCTION [dbo].[fComputeValue] (@data XML)
RETURNS datetime
WITH SCHEMABINDING
AS
BEGIN
RETURN @data.value('(/Metadata/Value[@Key="StartDate"])[1]', 'datetime')
END
However, when I try to create a calculated column:
ALTER TABLE dbo.CustomMetadataTable ADD [StartDate] AS ([dbo].[fComputeValue]([CustomMetadataColumn])) PERSISTED
I get the following error:
Msg 4936, Level 16, State 1, Line 2 The calculated column 'Start Date' in the CustomMetadataTable table cannot be saved because the column is non-deterministic.
It works if I:
- work with varchar, int, double values (i.e. other than datetime)
- remove the keyword PERSISTED (but then I cannot create an index in the column)
, datetime XSD.
? .