SQL 服务器格式化为 FOR XML
SQL Server Formatting with FOR XML
我有一个查询从数据库中的两个表中输出 XML 格式。
但是,我没有得到我想要的正确格式。
下面是查询:
SELECT m.MovieID AS MovieID,
m.MovieName AS MovieName,
mt.Description AS MovieType,
ISNULL((SELECT ms.ShowDateTime AS ShowDateTime
FROM MovieShowtime ms WITH(NOLOCK)
WHERE ms.MovieID = m.MovieID
ORDER BY ms.ShowDateTime ASC
FOR XML PATH('Showtime'), TYPE
), '') AS MovieShowtimes
FROM Movie m WITH(NOLOCK)
INNER JOIN MovieType mt WITH(NOLOCK)
ON mt.MovieTypeID = m.MovieTypeID
FOR XML PATH('Movie'), ROOT('Cinema'), TYPE
下面是输出:
<Cinema>
<Movie>
<MovieID>1234</MovieID>
<MovieName>Harry Potter and the Half-Blood Prince</MovieName>
<MovieType>Fantasy</MovieType>
<MovieShowtimes>
<Showtime>
<ShowDateTime>2019-08-26T19:30:00</ShowDateTime>
</Showtime>
<Showtime>
<ShowDateTime>2019-08-27T19:30:00</ShowDateTime>
</Showtime>
<Showtime>
<ShowDateTime>2019-08-28T19:30:00</ShowDateTime>
</Showtime>
</MovieShowtimes>
</Movie>
</Cinema>
但是,我的预期输出格式应如下所示:
<Cinema>
<Movie>
<MovieID>1234</MovieID>
<MovieName>Harry Potter and the Half-Blood Prince</MovieName>
<MovieType>Fantasy</MovieType>
<MovieShowtimes>
<ShowDateTime>2019-08-26T19:30:00</ShowDateTime>
<ShowDateTime>2019-08-27T19:30:00</ShowDateTime>
<ShowDateTime>2019-08-28T19:30:00</ShowDateTime>
</MovieShowtimes>
</Movie>
</Cinema>
我是 XML 的新手,所以我非常感谢您向正确的方向推动以正确设置格式。
只需从 MovieShowtimes 查询中删除 Path('Showtime')
并将其替换为 Path('')
select m.MovieID as MovieID
, m.MovieName as MovieName
, mt.Description as MovieType
, isnull(( select ms.ShowDateTime as ShowDateTime
from MovieShowtime as ms with (nolock)
where ms.MovieID = m.MovieID
order by ms.ShowDateTime asc
for xml path(''), type)
, '') as MovieShowtimes
from Movie as m with (nolock)
inner join MovieType as mt with (nolock) on mt.MovieTypeID = m.MovieTypeID
for xml path('Movie'), root('Cinema'), type
我有一个查询从数据库中的两个表中输出 XML 格式。 但是,我没有得到我想要的正确格式。
下面是查询:
SELECT m.MovieID AS MovieID,
m.MovieName AS MovieName,
mt.Description AS MovieType,
ISNULL((SELECT ms.ShowDateTime AS ShowDateTime
FROM MovieShowtime ms WITH(NOLOCK)
WHERE ms.MovieID = m.MovieID
ORDER BY ms.ShowDateTime ASC
FOR XML PATH('Showtime'), TYPE
), '') AS MovieShowtimes
FROM Movie m WITH(NOLOCK)
INNER JOIN MovieType mt WITH(NOLOCK)
ON mt.MovieTypeID = m.MovieTypeID
FOR XML PATH('Movie'), ROOT('Cinema'), TYPE
下面是输出:
<Cinema>
<Movie>
<MovieID>1234</MovieID>
<MovieName>Harry Potter and the Half-Blood Prince</MovieName>
<MovieType>Fantasy</MovieType>
<MovieShowtimes>
<Showtime>
<ShowDateTime>2019-08-26T19:30:00</ShowDateTime>
</Showtime>
<Showtime>
<ShowDateTime>2019-08-27T19:30:00</ShowDateTime>
</Showtime>
<Showtime>
<ShowDateTime>2019-08-28T19:30:00</ShowDateTime>
</Showtime>
</MovieShowtimes>
</Movie>
</Cinema>
但是,我的预期输出格式应如下所示:
<Cinema>
<Movie>
<MovieID>1234</MovieID>
<MovieName>Harry Potter and the Half-Blood Prince</MovieName>
<MovieType>Fantasy</MovieType>
<MovieShowtimes>
<ShowDateTime>2019-08-26T19:30:00</ShowDateTime>
<ShowDateTime>2019-08-27T19:30:00</ShowDateTime>
<ShowDateTime>2019-08-28T19:30:00</ShowDateTime>
</MovieShowtimes>
</Movie>
</Cinema>
我是 XML 的新手,所以我非常感谢您向正确的方向推动以正确设置格式。
只需从 MovieShowtimes 查询中删除 Path('Showtime')
并将其替换为 Path('')
select m.MovieID as MovieID
, m.MovieName as MovieName
, mt.Description as MovieType
, isnull(( select ms.ShowDateTime as ShowDateTime
from MovieShowtime as ms with (nolock)
where ms.MovieID = m.MovieID
order by ms.ShowDateTime asc
for xml path(''), type)
, '') as MovieShowtimes
from Movie as m with (nolock)
inner join MovieType as mt with (nolock) on mt.MovieTypeID = m.MovieTypeID
for xml path('Movie'), root('Cinema'), type