AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 115599
Accepted
World Wide DBA
World Wide DBA
Asked: 2015-09-21 14:54:20 +0800 CST2015-09-21 14:54:20 +0800 CST 2015-09-21 14:54:20 +0800 CST

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

  • 772

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

<xsd:element name="xid">
    <xsd:simpleType>
        <xsd:restriction base="xsd:string">
            <xsd:maxLength value="32" />
        </xsd:restriction>
    </xsd:simpleType>
</xsd:element>

您将如何使用 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

但这会产生以下错误:

消息 9301,级别 16,状态 1,行 2 XQuery [cm.item.data.modify()]:在此版本的服务器中,“cast as”不可用。请使用 '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

我收到此错误:

消息 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

我收到此错误:

Msg 9312, Level 16, State 1, Line 2 XQuery [cm.item.data.modify()]: 'text()' 不支持简单类型或' http://www.w3.org/2001/XMLSchema #anyType ' 元素,找到 '(element(ns{ http://www.anon.com/ }:xid,#anonymous) ?) *'。

我的目标是 SQL Server 2008 R2。

谢谢!

sql-server sql-server-2008-r2
  • 1 1 个回答
  • 2044 Views

1 个回答

  • Voted
  1. Best Answer
    Mikael Eriksson
    2015-09-22T03:49:43+08:002015-09-22T03:49:43+08:00

    我还没有找到一种简单的方法来修改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"');
    

    结果:

    消息 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?');
    

    另一种方法是将 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;
    
    • 7

相关问题

  • SQL Server - 使用聚集索引时如何存储数据页

  • 我需要为每种类型的查询使用单独的索引,还是一个多列索引可以工作?

  • 什么时候应该使用唯一约束而不是唯一索引?

  • 死锁的主要原因是什么,可以预防吗?

  • 如何确定是否需要或需要索引

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve