将查询导出到关系 xml
Export query to relational xml
在 Management Studio 中是否可以创建关系 XML 文件?
示例:
select a.product, a.cost, b.productType, c.brand
from products a
inner join productTypes b on a.productTypeID = b.productTypeID
inner join brands c on b.brandID = c.brandID
创建XML文件
<brands>
<brand>SomeBrand1
<productType>SomeProductType
<product>Product1</product>
<Cost>.00</cost>
</productType>
<brand>
<brand>SomeBrand2
<productType>SomeProductType
<product>Product3</product>
<Cost>.00</cost>
<product>Product4</product>
<Cost>.00</cost>
</productType>
<brand>
</brands>
谢谢,
乔
结构不完全一样,但结果是一样的:
select a.product, a.cost, b.productType, c.brand
from products a
inner join productTypes b on a.productTypeID = b.productTypeID
inner join brands c on b.brandID = c.brandID
FOR XML PATH ('brand'), TYPE, ROOT ('brands')
输出
<brands>
<brand>
<brand>SomeBrand1</brand>
<product>Product1</product>
<cost>10.00</cost>
<productType>SomeProductType</productType>
</brand>
<brand>
<brand>SomeBrand2</brand>
<product>Product3</product>
<cost>10.00</cost>
<productType>SomeProductType</productType>
</brand>
<brand>
<brand>SomeBrand2</brand>
<product>Product4</product>
<cost>10.00</cost>
<productType>SomeProductType</productType>
</brand>
</brands>
您可以使用嵌套来生成您想要的形状的 XML。此代码创建我使用的表。
--create products
select 'product1' product, 10.00 cost, 1 productTypeID into #Products
insert into #Products select 'product3', 10.00 cost, 2 productTypeID
insert into #Products select 'product4', 10.00 cost, 2 productTypeID
--create product types
select 1 productTypeID, 'SomeProductType1' productType, 1 brandID into #productTypes
insert into #productTypes select 2 productTypeID, 'SomeProductType2' productType, 2 brandID
--brands
select 1 brandID, 'SomeBrand1' brand into #brands
insert into #brands select 2 brandID, 'SomeBrand2' brand
我可能是错的,但我假设您需要两种不同的产品类型,所以我创建了其中两种。您的连接查询的结构方式,它具有从品牌到产品类型的一对多关系。这意味着每种产品类型只属于一个品牌。我想这两个品牌类型记录可能具有相同的名称,这将导致您在问题中引用的 XML。
下面的语句应该在您的示例中创建 XML 的形状:
select ltrim(brand),
(select LTRIM(productType),
(select product, cost
from #Products
where #Products.productTypeID = #productTypes.productTypeID
order by product
for XML PATH(''), type
)
from #productTypes
where #productTypes.brandID = #brands.brandID
order by productType
for XML path('productType'), type
)
from #brands
order by brand
for xml path('brand'), root('brands')
输出:
<brands>
<brand>SomeBrand1
<productType>SomeProductType1
<product>product1</product>
<cost>10.00</cost>
</productType>
</brand>
<brand>SomeBrand2
<productType>SomeProductType2
<product>product3</product>
<cost>10.00</cost>
<product>product4</product>
<cost>10.00</cost>
</productType>
</brand>
</brands>
最外面的 XML 路径语句 "for xml path('brand'), root('brands')" 将整个 XML 包装在一个名为 "brands" 的根中,然后为每一行创建名为 "brand" 的元素 return编辑。在 select 之后,我将 brand 列包装在一个没有别名的 ltrim 函数中,这样它就不会 return 一个列名,它插入内联文本而不在 [=35] 中为其创建标签=].
下一列 selected 是 return 产品类型的 XML 片段的子查询。 "Type" 关键字跟在该子查询的 XML 路径之后,表示要将其解释为 XML.
最里面的子查询只是 return 属于该产品类型的所有记录的产品和成本。通过指定 "for XML PATH('')" 表示不应为每一行创建 "row" 标记。
在 Management Studio 中是否可以创建关系 XML 文件?
示例:
select a.product, a.cost, b.productType, c.brand
from products a
inner join productTypes b on a.productTypeID = b.productTypeID
inner join brands c on b.brandID = c.brandID
创建XML文件
<brands>
<brand>SomeBrand1
<productType>SomeProductType
<product>Product1</product>
<Cost>.00</cost>
</productType>
<brand>
<brand>SomeBrand2
<productType>SomeProductType
<product>Product3</product>
<Cost>.00</cost>
<product>Product4</product>
<Cost>.00</cost>
</productType>
<brand>
</brands>
谢谢, 乔
结构不完全一样,但结果是一样的:
select a.product, a.cost, b.productType, c.brand
from products a
inner join productTypes b on a.productTypeID = b.productTypeID
inner join brands c on b.brandID = c.brandID
FOR XML PATH ('brand'), TYPE, ROOT ('brands')
输出
<brands>
<brand>
<brand>SomeBrand1</brand>
<product>Product1</product>
<cost>10.00</cost>
<productType>SomeProductType</productType>
</brand>
<brand>
<brand>SomeBrand2</brand>
<product>Product3</product>
<cost>10.00</cost>
<productType>SomeProductType</productType>
</brand>
<brand>
<brand>SomeBrand2</brand>
<product>Product4</product>
<cost>10.00</cost>
<productType>SomeProductType</productType>
</brand>
</brands>
您可以使用嵌套来生成您想要的形状的 XML。此代码创建我使用的表。
--create products
select 'product1' product, 10.00 cost, 1 productTypeID into #Products
insert into #Products select 'product3', 10.00 cost, 2 productTypeID
insert into #Products select 'product4', 10.00 cost, 2 productTypeID
--create product types
select 1 productTypeID, 'SomeProductType1' productType, 1 brandID into #productTypes
insert into #productTypes select 2 productTypeID, 'SomeProductType2' productType, 2 brandID
--brands
select 1 brandID, 'SomeBrand1' brand into #brands
insert into #brands select 2 brandID, 'SomeBrand2' brand
我可能是错的,但我假设您需要两种不同的产品类型,所以我创建了其中两种。您的连接查询的结构方式,它具有从品牌到产品类型的一对多关系。这意味着每种产品类型只属于一个品牌。我想这两个品牌类型记录可能具有相同的名称,这将导致您在问题中引用的 XML。
下面的语句应该在您的示例中创建 XML 的形状:
select ltrim(brand),
(select LTRIM(productType),
(select product, cost
from #Products
where #Products.productTypeID = #productTypes.productTypeID
order by product
for XML PATH(''), type
)
from #productTypes
where #productTypes.brandID = #brands.brandID
order by productType
for XML path('productType'), type
)
from #brands
order by brand
for xml path('brand'), root('brands')
输出:
<brands>
<brand>SomeBrand1
<productType>SomeProductType1
<product>product1</product>
<cost>10.00</cost>
</productType>
</brand>
<brand>SomeBrand2
<productType>SomeProductType2
<product>product3</product>
<cost>10.00</cost>
<product>product4</product>
<cost>10.00</cost>
</productType>
</brand>
</brands>
最外面的 XML 路径语句 "for xml path('brand'), root('brands')" 将整个 XML 包装在一个名为 "brands" 的根中,然后为每一行创建名为 "brand" 的元素 return编辑。在 select 之后,我将 brand 列包装在一个没有别名的 ltrim 函数中,这样它就不会 return 一个列名,它插入内联文本而不在 [=35] 中为其创建标签=].
下一列 selected 是 return 产品类型的 XML 片段的子查询。 "Type" 关键字跟在该子查询的 XML 路径之后,表示要将其解释为 XML.
最里面的子查询只是 return 属于该产品类型的所有记录的产品和成本。通过指定 "for XML PATH('')" 表示不应为每一行创建 "row" 标记。