如何在SQL Server中将分层数据转换为XML

huangapple go评论60阅读模式
英文:

How to Convert Hierarchical Data into XML in SQL Server

问题

以下是已翻译的内容:

<Geo>
	<Continent Name="Asia">
		<Country Name="China">
			<State Name="Bejing"></State>
		</Country>
		<Country Name="Japan">
			<State Name="Tokyo"></State>
		</Country>	
	</Continent>
	<Continent Name="Europe">
		<Country Name="Germany">
			<State Name="Berlin"></State>
		</Country>
		<Country Name="France">
			<State Name="Paris"></State>
		</Country>	
	</Continent>
</Geo>
英文:

sample Input Table Details:

如何在SQL Server中将分层数据转换为XML

Expected Output:

<Geo>
	<Continent Name="Asia">
		<Country Name="China">
			<State Name="Bejing"></State>
		</Country>
		<Country Name="Japan">
			<State Name="Tokyo"></State>
		</Country>	
	</Continent>
	<Continent Name="Europe">
		<Country Name="Germany">
			<State Name="Berlin"></State>
		</Country>
		<Country Name="France">
			<State Name="Paris"></State>
		</Country>	
	</Continent>
</Geo>

How to convert Input relation Data as Above Output XML?

答案1

得分: 1

以下是代码的翻译部分:

创建表 #continents (
	continentid int, [continent name] nvarchar(100))
插入到 #continents
选择	1, '亚洲'
联合全部
选择	2, '欧洲'
联合全部
选择	3, '非洲'

创建表 #countries (countryid int, [country name] nvarchar(100), continentid INT)

插入到 #countries
选择	1,'中国', 1
联合全部
选择	2, '日本', 1
联合全部
选择	3, '德国', 2
联合全部
选择	4, '法国', 2

创建表 #states (stateId INT, [State Name] nvarchar(100), countryid INT)

插入到 #states
选择	1, '东京', 2
联合全部
选择	2, '柏林', 3
联合全部
选择	3, '巴黎', 4
联合全部
选择	4, '北京', 1


选择	c.[Continent Name] AS [@Name]
,	(
	选择 cc.[Country Name] AS [@Name]
	,	(
		选择	[State Name] AS [@Name]
			#states ss
		在哪里	ss.countryId = cc.countryId
		FOR XML PATH('State'), TYPE
	)
		#countries cc
	在哪里	cc.continentId = c.continentId
	FOR XML PATH('Country'), TYPE
	)
	#continents c
FOR XML PATH('Continent'), ROOT('Geo'), TYPE

希望这有助于您理解代码的翻译部分。

英文:

Something like this:

create table #continents (
	continentid int, [continent name] nvarchar(100))
insert into #continents
SELECT	1, 'Asia'
UNION ALl
SELECT	2, 'Europe'
UNION ALL
SELECT	3, 'Africa'

CREATE TABLE #countries (countryid int, [country name] nvarchar(100), continentid INT)

insert into #countries
SELECT	1,'China', 1
UNION ALL
SELECT	2, 'Japan', 1
UNION ALL
SELECT	3, 'Germany', 2
UNION ALL
SELECT	4, 'France', 2

CREATE TABLE #states (stateId INT, [State Name] nvarchar(100), countryid INT)

INSERT INTO #states
SELECT	1, 'Tokya', 2
UNION ALL
SELECT	2, 'Berlin', 3
UNION ALL
SELECT	3, 'Paris', 4
UNION ALL
SELECT	4, 'Bejing', 1


select	c.[Continent Name] AS [@Name]
,	(
	select cc.[Country Name] AS [@Name]
	,	(
		SELECT	[State Name] AS [@Name]
		FROM	#states ss
		WHERE	ss.countryId = cc.countryId
		FOR XML PATH('State'), TYPE
	)
	FROM	#countries cc
	WHERE	cc.continentId = c.continentId
	FOR XML PATH('Country'), TYPE
	)
from	#continents c
for xml Path('Continent'), ROOT('Geo'), TYPE

Btw, next time, try to post data as create / insert script, instead of a screenshot, it really saves a lot of typing

答案2

得分: 1

I was just too late copying all the data....

<Geo>
	<Continent Continent_Name="Asia">
		<Country Country_Name="Japan">
			<State State_Name="Tokya"/>
		</Country>
	</Continent>
	<Continent Continent_Name="Europe">
		<Country Country_Name="Germany">
			<State State_Name="Berlin"/>
		</Country>
	</Continent>
</Geo>

更多信息:FOR XML (SQL Server)

英文:

I was just too late copying all the data....


WITH Continent AS (
SELECT 1 as Continent_ID, &#39;Asia&#39; as Continent_Name
UNION ALL
SELECT 2, &#39;Europe&#39;
UNION ALL
SELECT 3,&#39;Africa&#39;),
Country AS (
SELECT 1 as Country_ID, &#39;China&#39; as Country_Name, 1 as Continent_ID
UNION ALL
SELECT 2,&#39;Japan&#39;, 1
UNION ALL 
SELECT 3, &#39;Germany&#39;, 2),
State AS (
SELECT 1 as State_ID, &#39;Tokya&#39; as State_Name, 2 as Country_ID
UNION ALL
SELECT 2, &#39;Berlin&#39;, 3 )
select Continent_Name, Country_Name, State_Name
from Continent
inner join Country on Country.Continent_ID = Continent.Continent_ID
inner join State ON State.Country_ID = Country.Country_ID FOR XML AUTO, Root(&#39;Geo&#39;), TYPE;

output (after formatting):

&lt;Geo&gt;
&lt;Continent Continent_Name=&quot;Asia&quot;&gt;
&lt;Country Country_Name=&quot;Japan&quot;&gt;
&lt;State State_Name=&quot;Tokya&quot;/&gt;
&lt;/Country&gt;
&lt;/Continent&gt;
&lt;Continent Continent_Name=&quot;Europe&quot;&gt;
&lt;Country Country_Name=&quot;Germany&quot;&gt;
&lt;State State_Name=&quot;Berlin&quot;/&gt;
&lt;/Country&gt;
&lt;/Continent&gt;
&lt;/Geo&gt;

More info: FOR XML (SQL Server)

> A SELECT query returns results as a rowset. You can optionally
> retrieve formal results of a SQL query as XML by specifying the FOR
> XML clause in the query. The FOR XML clause can be used in top-level
> queries and in subqueries. The top-level FOR XML clause can be used
> only in the SELECT statement. In subqueries, FOR XML can be used in
> the INSERT, UPDATE, and DELETE statements. FOR XML can also be used in
> assignment statements.

huangapple
  • 本文由 发表于 2023年2月19日 23:43:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501305.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定