用 XQuery 替换 SQL Server 中强类型 xml 元素的值

Wor*_*DBA 11 xml sql-server sql-server-2008-r2 xquery

给定一个在 XML 模式集合中定义的元素,如下所示:

<xsd:element name="xid">
    <xsd:simpleType>
        <xsd:restriction base="xsd:string">
            <xsd:maxLength value="32" />
        </xsd:restriction>
    </xsd:simpleType>
</xsd:element>
Run Code Online (Sandbox Code Playgroud)

您将如何使用 XQuery 更新元素?

该元素位于架构集合的ns命名空间中。我一直在尝试更新以下查询的元素:

update cm.item
   set data.modify(
    'declare namespace ns="http://www.anon.com"; 
     replace value of (/ns:*/ns:xid)[1] with "X00011793" cast as element(ns{http://www.anon.com}:xid,#anonymous) ?') 
 where id = 11793
Run Code Online (Sandbox Code Playgroud)

但这会产生以下错误:

消息 9301,级别 16,状态 1,第 2 行 XQuery [cm.item.data.modify()]:在此版本的服务器中,“cast as”不可用。请使用“演员为?” 句法。

如果我完全删除演员表并使用此查询:

update cm.item
   set data.modify(
    'declare namespace ns="http://www.anon.com"; 
     replace value of (/ns:*/ns:xid)[1] with "X00011793"') 
 where id = 11793
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

消息 2247,级别 16,状态 1,第 2 行 XQuery [cm.item.data.modify()]:该值的类型为“xs:string”,它不是预期类型“<anonymous>”的子类型。

如果我发出这个查询:

update cm.item
   set data.modify(
      'declare namespace ns="http://www.anon.com/"; 
       replace value of (/ns:*/ns:xid/text())[1] with "X00011793"')
 where id = 11793
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

消息 9312,级别 16,状态 1,第 2 行 XQuery [cm.item.data.modify()]:简单类型或“ http://www.w3.org/2001/XMLSchema ”不支持“text()” #anyType ' 元素,找到 '(element(ns{ http://www.anon.com/ }:xid,#anonymous) ?) *'。

我的目标是 SQL Server 2008 R2。

谢谢!

Mik*_*son 7

我还没有找到一种简单的方法来修改replace value of语句以使用匿名简单类型定义。

简单再现你所拥有的:

drop xml schema collection dbo.SimpleTypeTest;

go

create xml schema collection dbo.SimpleTypeTest as 
N'<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="xid">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:maxLength value="30"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>';

go

declare @X xml(document dbo.SimpleTypeTest) = '<root><xid>1</xid></root>';

set @X.modify('replace value of /root/xid  with "2"');
Run Code Online (Sandbox Code Playgroud)

结果:

消息 2247,级别 16,状态 1,第 25 行 XQuery [modify()]:值的类型为“xs:string”,它不是预期类型“<anonymous>”的子类型。

一种解决方法是修改您的架构以使用命名的简单类型xidType并转换新值。

drop xml schema collection dbo.SimpleTypeTest;

go

create xml schema collection dbo.SimpleTypeTest as 
N'<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="xid" type="xidType"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:simpleType name="xidType">
        <xs:restriction base="xs:string">
            <xs:maxLength value="30"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>';

go

declare @X xml(document dbo.SimpleTypeTest) = '<root><xid>1</xid></root>';

set @X.modify('replace value of /root/xid  with "2" cast as xidType?');
Run Code Online (Sandbox Code Playgroud)

另一种方法是将 XML 提取到无类型的 XML 变量中,修改该变量并将其放回表中。

drop xml schema collection dbo.SimpleTypeTest;

go

create xml schema collection dbo.SimpleTypeTest as 
N'<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="xid">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:maxLength value="30"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>';

go

declare @X xml(document dbo.SimpleTypeTest) = '<root><xid>1</xid></root>';
declare @X2 xml = @X;

set @X2.modify('replace value of (/root/xid/text())[1]  with "2"');
set @X = @X2;
Run Code Online (Sandbox Code Playgroud)