英文:
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:
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, 'Asia' as Continent_Name
UNION ALL
SELECT 2, 'Europe'
UNION ALL
SELECT 3,'Africa'),
Country AS (
SELECT 1 as Country_ID, 'China' as Country_Name, 1 as Continent_ID
UNION ALL
SELECT 2,'Japan', 1
UNION ALL
SELECT 3, 'Germany', 2),
State AS (
SELECT 1 as State_ID, 'Tokya' as State_Name, 2 as Country_ID
UNION ALL
SELECT 2, 'Berlin', 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('Geo'), TYPE;
output (after formatting):
<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>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论