XML查询示例四

分离XML

DECLARE @xml XML

SET @xml = (
       SELECT  TOP 2 Name, 
           BusinessEntityID AS ID, 
           Demographics.query('/')
       FROM Sales.Store AS Store
       WHERE SalesPersonID = 282
       FOR XML AUTO, ROOT('StoreSurveys')
       )

SELECT @xml 
;WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/StoreSurvey' AS ns)
SELECT C.value('../@ID','int') AS BusinessEntityID,
   C.value('../@Name','varchar(50)') AS StoreName,
   C.query('.') AS Demographics
FROM @xml.nodes('/StoreSurveys/Store/ns:StoreSurvey') AS T(C)
GO


BusinessEntityID StoreName Demographics
312 Vinyl and Plastic Goods Corporation <StoreSurvey xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/StoreSurvey"><AnnualSales>1500000</AnnualSales><AnnualRevenue>150000</AnnualRevenue><BankName>Primary Bank &amp; Reserve</BankName><BusinessType>OS</BusinessType><YearOpened>1980</YearOpened><Specialty>Mountain</Specialty><SquareFeet>41000</SquareFeet><Brands>4+</Brands><Internet>DSL</Internet><NumberEmployees>43</NumberEmployees></StoreSurvey>
324 Valley Toy Store <StoreSurvey xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/StoreSurvey"><AnnualSales>300000</AnnualSales><AnnualRevenue>30000</AnnualRevenue><BankName>Reserve Security</BankName><BusinessType>BM</BusinessType><YearOpened>1979</YearOpened><Specialty>Mountain</Specialty><SquareFeet>9000</SquareFeet><Brands>2</Brands><Internet>T1</Internet><NumberEmployees>6</NumberEmployees></StoreSurvey>

 

DECLARE @xml XML

SET @xml = (
       SELECT  TOP 2 Name, 
           BusinessEntityID AS ID, 
           Demographics.query('/')
       FROM Sales.Store AS Store
       WHERE SalesPersonID = 282
       FOR XML AUTO, ROOT('StoreSurveys')
       )

SELECT @xml 
SELECT C.value('@ID','int') AS BusinessEntityID,
   C.value('@Name','varchar(50)') AS StoreName,
   C.query('./child::node()') AS Demographics
FROM @xml.nodes('/StoreSurveys/Store') AS T(C)
GO 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值