以XML格式检索查询结果的存储过程

时间:2015-04-20 12:05:44

标签: java xml stored-procedures

是否可以以XML格式检索存储过程结果?我使用Java调用存储过程和Microsoft SQL Server管理工作室来测试我的存储过程。有人可以提供示例代码吗?

1 个答案:

答案 0 :(得分:0)

找到这样的东西 SELECT CustomerID AS '@CustomerID', CustName AS '@Name', (SELECT ProductName AS '@productname' FROM dbo.Products p WHERE p.CustomerID = c.CustomerID
FOR XML PATH('Product'), TYPE) AS 'Products', (SELECT HobbyName AS '@hobbyname' FROM dbo.Hobbies h WHERE h.CUstomerID = c.CustomerID FOR XML PATH('Hobby'), TYPE) AS 'Hobbies' FROM dbo.Customers c FOR XML PATH('Customer'), ROOT('Customers')

提供以下输出

<Customers>
  <Customer CustomerID="1" Name="Fred">
    <Products>
      <Product productname="Table" />
      <Product productname="Wardrobe" />
      <Product productname="Chair" />
    </Products>
    <Hobbies>
      <Hobby hobbyname="Golf" />
      <Hobby hobbyname="Swimming" />
    </Hobbies>
  </Customer>
  <Customer CustomerID="2" Name="Sue">
    <Products>
      <Product productname="CD Player" />
      <Product productname="Picture frame" />
    </Products>
    <Hobbies>
      <Hobby hobbyname="Dancing" />
      <Hobby hobbyname="Gardening" />
      <Hobby hobbyname="Reading" />
    </Hobbies>
  </Customer>
</Customers>

这是对的吗?