【问题标题】:SQL fetch xml data attribute and values from non xml type columnSQL 从非 xml 类型列中获取 xml 数据属性和值
【发布时间】:2015-03-20 08:37:24
【问题描述】:

如何使用 SQL 从非 xml 类型的列中检索 xml 类型的数据 我有一张桌子和里面的一列 xml 是 ntext 类型的列

xml列数据示例如下

<message to="4075@abc.myftp.org" type="chat" from="5082@abc.myftp.org/e76bea0f">
<body>james bond</body>
<active xmlns="http://jabber.org/protocol/chatstates" />
</message>

我想获取“to”、“from”属性值和标签值

请推荐

【问题讨论】:

    标签: sql sql-server sql-server-2008 openfire sqlxml


    【解决方案1】:

    你已经错过了xml中的结束标签&lt;/message&gt;,但是如果你添加它,你可以使用value()函数:

    declare @temp table (data ntext)
    
    insert into @temp (data)
    select '<message to="4075@abc.myftp.org" type="chat" from="5082@abc.myftp.org/e76bea0f">
    <body>james bond</body>
    <active xmlns="http://jabber.org/protocol/chatstates" />
    </message>'
    
    select
        c.data.value('message[1]/@to', 'nvarchar(max)')
    from @temp as t
        outer apply (select cast(data as xml)) as c(data)
    

    【讨论】:

    • 感谢@Roman 的完美回答和快速响应
    • 嗨 Roman,我正在尝试从 PHP 应用程序运行此查询,但收到错误 /***** [nativecode=1934 - SELECT failed 因为以下 SET 选项的设置不正确:'ANSI_NULLS, QUOTED_IDENTIFIER、CONCAT_NULL_YIELDS_NULL、ANSI_WARNINGS、ANSI_PADDING'。验证 SET 选项是否适用于索引视图和/或计算列上的索引和/或过滤索引和/或查询通知和/或 XML 数据类型方法和/或空间索引操作。] *****/ 可以你请建议
    猜你喜欢
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多