2013-06-22 277 views
3

我是SQL Server和T-SQL的新手。我如何查询存储在SQL Server 2008 R2中这个xml中的信息?在SQL Server 2008 R2中查询Xml

XML:

<smp:Root xmlns:smp="http://tempuri.org/smp.xsd" header="Test Title"> 
    <smp:Sections> 
    <smp:G3 idnumber="01"> 
     <SectionHost>ABC</SectionHost> 
    </smp:G3> 
    <smp:G2 idnumber="01"> 
     <SectionHost>DEF</SectionHost> 
    </smp:G2> 
    </smp:Sections> 
</smp:Root> 
+0

我为Xpath函数添加了另一个链接,它们也可以提供帮助。 – KeyboardFriendly

回答

3

如果您有存储在XmlXml只使用value methodshredXml

下一次尝试和发布一些DDL, DML向我们展示你已经尝试了什么,你的表结构等

但试试这个

WITH XMLNAMESPACES (Default 'http://tempuri.org/smp.xsd') 
SELECT  
    a.value('@header', 'nvarchar(50)') as Header, 
    b.value('local-name(.)', 'nvarchar(50)') as Sections, 
    b.value('@idnumber' ,'int') as IdNumber, 
    b.value('.' , 'nvarchar(20)') as Host 

From ATable As x 

       Cross Apply x.AXmlColumn.nodes('Root') a(a) 
           Cross Apply a.nodes('Sections/*') b(b) 

以下是一些有用links让你开始:

https://www.simple-talk.com/sql/learn-sql-server/the-xml-methods-in-sql-server/

http://blog.sqlauthority.com/2008/01/15/sql-server-what-is-dml-ddl-dcl-and-tcl-introduction-and-examples/

http://msdn.microsoft.com/en-us/library/ms189254.aspx