SET ARITHABORT ON;
DECLARE @xml XML;
SET @xml = CAST('<ArrayOfServicePopulation><ServicePopulation><PopulationId>1</PopulationId><PopulationName>Administrators</PopulationName><IsOther>false</IsOther></ServicePopulation><ServicePopulation><PopulationId>2</PopulationId><PopulationName>Adults</PopulationName><IsOther>false</IsOther></ServicePopulation><ServicePopulation><PopulationId>3</PopulationId><PopulationName>Business and Industry</PopulationName><IsOther>false</IsOther></ServicePopulation><ServicePopulation><PopulationId>4</PopulationId><PopulationName>Children of Substance Abusers*</PopulationName><IsOther>false</IsOther></ServicePopulation><ServicePopulation><PopulationId>5</PopulationId><PopulationName>Civic Groups/Coalitions</PopulationName><IsOther>false</IsOther></ServicePopulation></ArrayOfServicePopulation>' AS XML);
SELECT servicePopulations.value('.', 'varchar(max)') AS [populations]
FROM @xml.nodes('/ArrayOfServicePopulation/ServicePopulation') results ( servicePopulations )
当我尝试上述操作时,我将所有内容都排成行,但不是单独的列。所有数据都汇总到一列,如下所示:
我对xquery并不熟悉,似乎无法找到如何做到这一点的例子。任何帮助,将不胜感激。我觉得我很亲密......但我觉得我也很远。
答案 0 :(得分:2)
试试这个:
DECLARE @xml XML;
SET @xml = '<ArrayOfServicePopulation>
<ServicePopulation>
<PopulationId>1</PopulationId>
<PopulationName>Administrators</PopulationName>
<IsOther>false</IsOther>
</ServicePopulation>
<ServicePopulation>
<PopulationId>2</PopulationId>
<PopulationName>Adults</PopulationName>
<IsOther>false</IsOther>
</ServicePopulation><ServicePopulation><PopulationId>3</PopulationId><PopulationName>Business and Industry</PopulationName><IsOther>false</IsOther></ServicePopulation><ServicePopulation><PopulationId>4</PopulationId><PopulationName>Children of Substance Abusers*</PopulationName><IsOther>false</IsOther></ServicePopulation><ServicePopulation><PopulationId>5</PopulationId><PopulationName>Civic Groups/Coalitions</PopulationName><IsOther>false</IsOther></ServicePopulation></ArrayOfServicePopulation>'
SELECT
PopulationId = servicePopulations.value('(PopulationId)[1]', 'int'),
PopulationName = servicePopulations.value('(PopulationName)[1]', 'varchar(50)'),
IsOther = servicePopulations.value('(IsOther)[1]', 'varchar(10)')
FROM
@xml.nodes('/ArrayOfServicePopulation/ServicePopulation') results ( servicePopulations )
那应该给你一个输出:
PopulationId PopulationName IsOther
-------------------------------------------------------
1 Administrators false
2 Adults false
3 Business and Industry false
4 Children of Substance Abusers* false
5 Civic Groups/Coalitions false