zoukankan      html  css  js  c++  java
  • xml Data Type Methods in sql server

    nodes() Method (xml Data Type)

    https://docs.microsoft.com/en-us/sql/t-sql/xml/nodes-method-xml-data-type

     The nodes() method is useful when you want to shred an xml data type instance into relational data.

    It allows you to identify nodes that will be mapped into a new row.

     

    Every xml data type instance has an implicitly provided context node.

    For the XML instance stored in a column or variable, this is the document node.

    The document node is the implicit node at the top of every xml data type instance.

     

    The result of the nodes() method is a rowset that contains logical copies of the original XML instances.

    In these logical copies, the context node of every row instance is set to one of the nodes identified with the query expression, so that subsequent queries can navigate relative to these context nodes.

     

    You can retrieve multiple values from the rowset.

    For example, you can apply the value() method to the rowset returned by nodes() and retrieve multiple values from the original XML instance.

    Note that the value() method, when applied to the XML instance, returns only one value.  

    Syntax

    nodes (XQuery) as Table(Column)  

    Arguments

    XQuery
    Is a string literal, an XQuery expression.

    If the query expression constructs nodes, these constructed nodes are exposed in the resulting rowset.

    If the query expression results in an empty sequence, the rowset will be empty.

    If the query expression statically results in a sequence that contains atomic values instead of nodes, a static error is raised.

     

    Table(Column)
    Is the table name and the column name for the resulting rowset.  

    Example

     DECLARE @UsedRecords XML;
    SET @UsedRecords = '<Record ID="107" /><Record ID="116" /><Record ID="410" />';
     
     SELECT Result.Id.value(
                                                                             '@ID' ,
                                                                             'int'
                                                                         )
                                                      FROM   @UsedRecords.nodes('/Record') AS Result(Id)

    query() Method (xml Data Type)

    https://docs.microsoft.com/en-us/sql/t-sql/xml/query-method-xml-data-type

    declare @myDoc xml  
    set @myDoc = '<Root>  
    <ProductDescription ProductID="1" ProductName="Road Bike">  
    <Features>  
      <Warranty>1 year parts and labor</Warranty>  
      <Maintenance>3 year parts and labor extended maintenance is available</Maintenance>  
    </Features>  
    </ProductDescription>  
    </Root>'  
    SELECT @myDoc.query('/Root/ProductDescription/Features')  
    DECLARE @Propertis XML;
    SELECT @Propertis = WebPartProperties
    FROM   dbo.CMS_WebPart
    WHERE  WebPartName = 'LISA_GiftBrowser';
    SELECT @Propertis;
    SELECT @Propertis.query('/form/field/settings/controlname')

    参考

    https://stackoverflow.com/questions/15680259/parse-xml-in-sql-server

    DECLARE @xml xml
    SET @xml = 
    '<GespeicherteDaten>
    <strategieWuerfelFelder Type="strategieWuerfelFelder">
        <Felder X="3" Y="3" Z="3">
            <Feld X="1" Y="1" Z="1">
                <strategieWuerfelFeld Type="strategieWuerfelFeld">
                    <Name>Name</Name>
                    <Beschreibung>Test</Beschreibung>
                </strategieWuerfelFeld>
            </Feld>
            <Feld X="1" Y="1" Z="2">
                <strategieWuerfelFeld Type="strategieWuerfelFeld">
                    <Name>Name2</Name>
                    <Beschreibung>Test2</Beschreibung>
                </strategieWuerfelFeld>
            </Feld>
        </Felder>
    </strategieWuerfelFelder>
    </GespeicherteDaten>
    ' SELECT b.value('@X', 'int') as X , b.value('@Y', 'int') as Y , b.value('@Z', 'int') as Z , b.value('(./strategieWuerfelFeld/Name/text())[1]','Varchar(50)') as [Name] , b.value('../@X','int') as Felder_X , b.value('../@Y','int') as Felder_Y , b.value('../@Z','int') as Felder_Z FROM @xml.nodes('/GespeicherteDaten/strategieWuerfelFelder/Felder/Feld') as a(b)
    <field column="CoverAllGiftBrand" fieldcaption="Cover All Brand" visible="true" columntype="text" fieldtype="CustomUserControl" allowempty="true" columnsize="50" fielddescription="A brand which can cover all brands of gift" publicfield="false" guid="734db2ca-5ce3-4326-a5c8-a821631e09ae" visibility="none">
    <settings>
    <controlname>textboxcontrol</controlname>
    </settings>
    </field>
    DECLARE @Propertis XML;
    DECLARE @Result XML;
    SELECT @Propertis = WebPartProperties
    FROM   dbo.CMS_WebPart
    WHERE  WebPartName = 'LISA_GiftQuickSearch';
    SELECT 
    b.value('@fieldcaption','nvarchar(50)') AS Property
    ,b.value('@column','nvarchar(50)') AS ColumnName
    ,b.value('(./settings/controlname/text())[1]', 'nvarchar(50)') AS ControlType
    FROM   @Propertis.nodes('/form/field') AS a(b)
    WHERE b.value('(./settings/controlname/text())[1]', 'nvarchar(50)') LIKE '%lisa%'
    ORDER BY ControlType;
    DECLARE @Propertis XML;
    DECLARE @Result XML;
    SELECT @Propertis = WebPartProperties
    FROM   dbo.CMS_WebPart
    WHERE  WebPartName = 'LISA_GiftBrowser';
    SELECT 
    DISTINCT b.value('(./settings/controlname/text())[1]', 'nvarchar(100)') AS ControlType
    FROM   @Propertis.nodes('/form/field') AS a(b)
    WHERE b.value('(./settings/controlname/text())[1]', 'nvarchar(100)') LIKE '%lisa%'
    ORDER BY ControlType;

    Parse List

    DECLARE @Parameters XML = '
    <Request xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Orders>
        <Order>
          <Column>Column1</Column>
          <Direction>Asc</Direction>
        </Order>
        <Order>
          <Column>Column2</Column>
          <Direction>Desc</Direction>
        </Order>
      </Orders>
      <AccountId>1</AccountId>
    </Request>
    ';
    DECLARE @AccountId INT;
    
    SELECT @AccountId = b.value('(./AccountId/text())[1]', 'INT')
    FROM   @Parameters.nodes('/Request') AS a(b);
    SELECT @AccountId AS AccountId
    
    SELECT b.value('(./Column/text())[1]', 'NVARCHAR(50)') AS ColumnName ,
           b.value('(./Direction/text())[1]', 'NVARCHAR(50)') AS Direction
    FROM   @Parameters.nodes('/Request/Orders/Order') AS a(b);

    parse audit criteria

    DECLARE @Params XML
        = N'<AuditCriteria xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><ProgramName /><UserName /><ActionType /></AuditCriteria>';
        
    SELECT b.value('(./ProgramName/text())[1]', 'NVarchar(max)'),
           b.value('(./UserName/text())[1]', 'NVarchar(max)'),
           b.value('(./ActionType/text())[1]', 'NVarchar(max)')
    FROM @Params.nodes('/AuditCriteria') AS a(b);
  • 相关阅读:
    【转】C#连接mysql
    【转】深度优先算法
    【转】mysql安装
    win7NVIDIA显卡驱动升级时卡住
    【转】win7系统删除桌面IE图标
    双系统删掉一个后,所在分区无法格式化
    SQL各种JOIN
    C# 反射
    【转】C#强制转换和显式转换
    SQL Server 去除表中字段空格
  • 原文地址:https://www.cnblogs.com/chucklu/p/7338066.html
Copyright © 2011-2022 走看看