【问题标题】:SQL Server xml column filteringSQL Server xml 列过滤
【发布时间】:2014-05-07 15:41:16
【问题描述】:

我有一个带有 xml 列的 SQL Server 表。 xml 列有以下 xml 数据。

<organisation>
    <organisation id="1"> <p> Content by Interviewee1 </p1> <p> Interviewee1 is talking about organisation-1 </p1> </organisation>
    <organisation id="2"> <p> Content by Interviewee2 </p1> <p> Interviewee2 is talking about organisation-2 </p1> </organisation>
    <organisation id="1"> <p> Content by Interviewee3 </p1> <p> Interviewee3 is talking about organisation-1 </p1> </organisation>
</organisation>

我需要按组织 ID 过滤。例如,如果我按 Id = 1 过滤,则预期会出现以下 xml。

<organisation>
    <organisation id="1"> <p> Content by Interviewee1 </p1> <p> Interviewee1 is talking about organisation-1 </p1> </organisation>
    <organisation id="1"> <p> Content by Interviewee3 </p1> <p> Interviewee3 is talking about organisation-1 </p1> </organisation>
</organisation>

【问题讨论】:

  • 您的意思是您需要过滤SELECT 还是尝试实际更新列并在此过程中过滤XML?
  • 我需要过滤。当有人说,获取组织 id = 1 的所有面试时,我需要获取组织 id = 1 的面试数据。

标签: sql xml sql-server-2005 sqlxml


【解决方案1】:

这是一种方法:

DECLARE @T TABLE (X XML)

INSERT INTO @T
        ( X )
VALUES  ( '<organisation>
    <organisation id="1"> <p> Content by Interviewee1 </p> <p1> Interviewee1 is talking about organisation-1 </p1> </organisation>
    <organisation id="2"> <p> Content by Interviewee2 </p> <p1> Interviewee2 is talking about organisation-2 </p1> </organisation>
    <organisation id="1"> <p> Content by Interviewee3 </p> <p1> Interviewee3 is talking about organisation-1 </p1> </organisation>
</organisation>'
          )

DECLARE @ID INT = 1


SELECT X.query('<organisation> 
                {for $o in /organisation/organisation
                where $o[@id=sql:variable("@ID")]
                return $o
                } </organisation>' )
FROM @T 

【讨论】:

    【解决方案2】:

    这是另一个...我将 CDATA 标记添加到您的 XML 节点以保留 html 标记,但我不知道这是否适合您。希望它有所帮助(Stuart Ainsworth 提交的第一个答案看起来比这个更干净更好)

    DECLARE @columnData XML =
    '<organisation>
        <organisation id="1"> <![CDATA[ <p> Content by Interviewee1 </p> <p> Interviewee1 is talking about organisation-1 </p> ]]></organisation>
        <organisation id="2"> <![CDATA[ <p> Content by Interviewee2 </p> <p> Interviewee2 is talking about organisation-2 </p> ]]></organisation>
        <organisation id="1"> <![CDATA[ <p> Content by Interviewee3 </p> <p> Interviewee3 is talking about organisation-1 </p> ]]></organisation>
    </organisation>'
    
    DECLARE @temporaryXMLtable TABLE (organisation XML)
    INSERT INTO @temporaryXMLtable
    SELECT 
    fctn.arg.query('.')
    FROM @columnData.nodes('/organisation/organisation')
    fctn(arg)
    
    DECLARE @filteredXML XML
    SET @filteredXML = (SELECT 
            t.organisation.value('(organisation/@id)[1]','VARCHAR(MAX)') AS '@id',
            t.organisation.value('organisation[1]','VARCHAR(MAX)')
        FROM (SELECT organisation from @temporaryXMLtable WHERE (SELECT     organisation.value('(organisation/@id)[1]','VARCHAR(20)')) <> 2)
        t(organisation)
        FOR XML PATH('organisation'), ROOT('organisation'))
    
    SELECT @filteredXML
    

    【讨论】:

      猜你喜欢
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      • 1970-01-01
      相关资源
      最近更新 更多