2

我正在使用 SQL Server 2008 R2 和 ASP.NET 3.5。我在 SQL 端以 XML 形式存储数据表 (.NET)。列的数据类型为 XML。

这是将 XML 存储在 SQL Server 表列中的示例。

<container>
<NewDataSet>

<Containers>
  <BARCODE>680812</BARCODE>
  <CATEGORYID>114</CATEGORYID>
  <NAME>KS000147</NAME>
  <GWT>4.640</GWT>
  <NWT>4.640</NWT>
  <LAM>30.00</LAM>
  <QTY>2</QTY>
  <QUANTITY>1</QUANTITY>
  <REMARKS>HM</REMARKS>
</Containers>
<Containers>
  <BARCODE>680813</BARCODE>
  <CATEGORYID>114</CATEGORYID>
  <NAME>KS000147</NAME>
  <GWT>4.680</GWT>
  <NWT>4.680</NWT>
  <LAM>30.00</LAM>
  <QTY>2</QTY>
  <QUANTITY>1</QUANTITY>
  <REMARKS>HM</REMARKS>
</Containers>
<Containers>
  <BARCODE>680814</BARCODE>
  <CATEGORYID>114</CATEGORYID>
  <NAME>KS000147</NAME>
  <GWT>4.490</GWT>
  <NWT>4.490</NWT>
  <LAM>30.00</LAM>
  <QTY>2</QTY>
  <QUANTITY>1</QUANTITY>
  <REMARKS>HM</REMARKS>
</Containers>
</NewDataSet>
</container>

现在我想以表格形式获取这个 XML。如何在 SQL 中编写查询以获取以下表格格式的 XML?IE

BARCODE | CATEGORYID |NAME     |GWT   |NWT   |LAM   |QTY |QUANTITY |REMARKS 
680814  |    114     |KS000147 |4.490 |4.490 |30.00 | 2  |   1     |HM

如果您需要更多详细信息,请告诉我。

4

2 回答 2

2

你可以尝试这样的事情:

SELECT
    Barcode = Container.value('(BARCODE)[1]', 'int'),
    CategoryID = Container.value('(CATEGORYID)[1]', 'int'),
    Name = Container.value('(NAME)[1]', 'varchar(50)'),
    GWT = Container.value('(GWT)[1]', 'decimal(10, 3)'),
    NWT = Container.value('(NWT)[1]', 'decimal(10, 3)'),
    LAM = Container.value('(LAM)[1]', 'decimal(10, 3)'),
    QTY = Container.value('(QTY)[1]', 'int'),
    Quantity = Container.value('(QUANTITY)[1]', 'int'),
    Remarks = Container.value('(REMARKS)[1]', 'varchar(50)')
FROM
    dbo.YourTableHere
CROSS APPLY 
    XmlColumnHere.nodes('/container/NewDataSet/Containers') AS T(Container)
WHERE
    ID = 1

这将产生类似这样的输出(对于您给定的示例数据):

在此处输入图像描述

于 2012-05-12T07:50:05.540 回答
0

此示例中的 XML 文档包含 <customer>、<order> 和 <orderdetail> 元素。

DECLARE @DocHandle int
Declare @XML NVARCHAR(MAX)
SET @XML = '<ROOT>
<Customer CustomerID="1" ContactName="vibhav bhavsar>
<Order OrderID="101" CustomerID="1" OrderDate="2014-01-01">
<OrderDetail ProductID="16" Quantity="11" Amount="200$">
One of the best customer
</OrderDetail>
<OrderDetail ProductID="57" Quantity="6" Amount="150$"/>
</Order>
</Customer>
<Customer CustomerID="2" ContactName="jay bhavsar">
<Order OrderID="102" CustomerID="2" OrderDate="2014-02-01">
<OrderDetail ProductID="12" Quantity="9" Amount="180$">
Customer was very satisfied
</OrderDetail>
<OrderDetail ProductID="7" Quantity="2" Amount="50$"/>
</Order>
</Customer> </ROOT>'

--Need to create an internal representation of the XML document.
EXEC sp_xml_preparedocument @DocHandle OUTPUT, @XML

--Execute a SELECT statement using OPENXML.
SELECT * 
FROM OPENXML (@DocHandle, '/ROOT/Customer/Order/OrderDetail')
WITH (OrderID int '../@OrderID',
CustomerID  varchar(10) '../../@CustomerID',
ContactName varchar(100) '../../@ContactName',
OrderDate   datetime '../@OrderDate',
ProductID  int '@ProductID',
Qty int '@Quantity',
Amount varchar(10) '@Amount',
Comment varchar(50) 'text()')

SELECT 语句用于检索 OPENXML 提供的行集中的所有列。

查看更多点击这里

于 2014-04-05T20:13:15.047 回答