SQL 到 XML 格式化
SQL to XML Formatting
我正在尝试为 SQL 查询连接三个表以输出为 XML
以下是我的查询:
select
invoices.order_no as "order",
customerinfo.name as BillingCompanyName,
line.item_code,
line.item_qty
from
invoices
inner join
customerinfo on customerinfo.cust_code = invoices.cust_code
inner join
line on line.order_no = invoices.order_no
where
invoices.custlog10 = 'f' and invoices.order_no = '332504'
for xml auto, type, elements
这是生成的输出
<invoices>
<order>332504</order>
<customerinfo>
<BillingCompanyName>Two Bear Farm</BillingCompanyName>
<line>
<item_code>2909-B</item_code>
<item_qty>2.000000</item_qty>
</line>
<line>
<item_code>SH-DISC</item_code>
<item_qty>1.000000</item_qty>
</line>
</customerinfo>
</invoices>
此输出大部分是正确的,我只是将其移动到 customerinfo
标记结束的位置,如下所示,以粗体显示:
<invoices>
<order>332504</order>
<customerinfo>
<BillingCompanyName>Two Bear Farm</BillingCompanyName>
</customerinfo>
<line>
<item_code>2909-B</item_code>
<item_qty>2.000000</item_qty>
</line>
<line>
<item_code>SH-DISC</item_code>
<item_qty>1.000000</item_qty>
</line>
</invoices>
此代码旨在提供给第三方模块以读取销售订单。
我是 SQL 输出为 XML 的新手,任何解决此格式问题的帮助都会很棒!
一种方法是通过具有显式路径的相关查询,因为您可以更好地控制格式:
select
invoices.order_no as "order",
(select customerinfo.name as 'BillingCompanyName'
from customerinfo where
customerinfo.cust_code=invoices.cust_code
for xml path('customerinfo'), type, elements
),
(select line.item_code, line.item_qty
from line where line.order_no = invoices.order_no
for xml path('line'), type, elements)
from
invoices
where
invoices.custlog10 = 'f' and invoices.order_no = '332504'
for xml path('invoices')
我正在尝试为 SQL 查询连接三个表以输出为 XML 以下是我的查询:
select
invoices.order_no as "order",
customerinfo.name as BillingCompanyName,
line.item_code,
line.item_qty
from
invoices
inner join
customerinfo on customerinfo.cust_code = invoices.cust_code
inner join
line on line.order_no = invoices.order_no
where
invoices.custlog10 = 'f' and invoices.order_no = '332504'
for xml auto, type, elements
这是生成的输出
<invoices>
<order>332504</order>
<customerinfo>
<BillingCompanyName>Two Bear Farm</BillingCompanyName>
<line>
<item_code>2909-B</item_code>
<item_qty>2.000000</item_qty>
</line>
<line>
<item_code>SH-DISC</item_code>
<item_qty>1.000000</item_qty>
</line>
</customerinfo>
</invoices>
此输出大部分是正确的,我只是将其移动到 customerinfo
标记结束的位置,如下所示,以粗体显示:
<invoices>
<order>332504</order>
<customerinfo>
<BillingCompanyName>Two Bear Farm</BillingCompanyName>
</customerinfo>
<line>
<item_code>2909-B</item_code>
<item_qty>2.000000</item_qty>
</line>
<line>
<item_code>SH-DISC</item_code>
<item_qty>1.000000</item_qty>
</line>
</invoices>
此代码旨在提供给第三方模块以读取销售订单。
我是 SQL 输出为 XML 的新手,任何解决此格式问题的帮助都会很棒!
一种方法是通过具有显式路径的相关查询,因为您可以更好地控制格式:
select
invoices.order_no as "order",
(select customerinfo.name as 'BillingCompanyName'
from customerinfo where
customerinfo.cust_code=invoices.cust_code
for xml path('customerinfo'), type, elements
),
(select line.item_code, line.item_qty
from line where line.order_no = invoices.order_no
for xml path('line'), type, elements)
from
invoices
where
invoices.custlog10 = 'f' and invoices.order_no = '332504'
for xml path('invoices')