我正在学习 XML/SQL 并有以下问题。我想将 XML 数据分解成一个表。但我的问题是:我有许多作者<author>
在同一书籍类别中使用相同的标签 ()。我想选择所有作者,但我无法做到这一点。你能帮我这样做吗?
结果应如下所示:
category title author author1 author2
=============================================================================
CHILDREN Harry Potter J K. Rowling NULL NULL
WEB XQuery Kick Start James McGovern Per Bothner Kurt Cagle
代码:
declare @int int
declare @var xml = '<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<year>2003</year>
<price>49.99</price>
</book>
</bookstore>';
EXEC sp_xml_preparedocument @int OUTPUT, @var
SELECT
*
into MyTable
FROM
OPENXML(@int, 'bookstore/book', 11)
with
(
category varchar(100),
title varchar(100),
author varchar(100)
author1 varchar(100),
author2 varchar(100)
);