我相信在这个话题上有很多帖子,但到目前为止还没有任何内容可以提供给我需要的地方......
基本上我正在尝试从SQL重新创建以下XML输出文件。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<inventory company_acro_name="TEST" company_mail_to="" hold_on_error="N">
<product recordtype="INVENTORY" quantity="48" supplierassortmentid="4916464_811-6.5" date="2018-04-25 13:29:32" />
<product recordtype="INVENTORY" quantity="89" supplierassortmentid="4916464_811-7" date="2018-04-25 13:29:32" />
<product recordtype="INVENTORY" quantity="136" supplierassortmentid="4916464_811-7.5" date="2018-04-25 13:29:32" />
<product recordtype="INVENTORY" quantity="57" supplierassortmentid="4916464_811-8" date="2018-04-25 13:29:32" />
<product recordtype="INVENTORY" quantity="54" supplierassortmentid="4916464_811-8.5" date="2018-04-25 13:29:32" />
<product recordtype="INVENTORY" quantity="46" supplierassortmentid="4916464_811-9" date="2018-04-25 13:29:32" />
<product recordtype="INVENTORY" quantity="50" supplierassortmentid="4916464_811-9.5" date="2018-04-25 13:29:32" />
</inventory>
到目前为止,我已设法使用以下脚本生成数据部分
SELECT
1 as [Tag], 0 as [Parent],
'INVENTORY' as [Product!1!recordtype],
CAST([Available_Quantity] as int) as [Product!1!quantity],
[Product] + '_' + [Matrix_Code_1] + '-' + [Matrix_Code_2] as [Product!1!supplierassortmentid],
CONVERT(smalldatetime,CURRENT_TIMESTAMP) as [Product!1!date]
FROM
[dbo].[TStock]
WHERE
Company = 4
FOR XML EXPLICIT
但是我很难添加所需文件的前两行
可以这样做吗?
谢谢
答案 0 :(得分:0)
FOR XML EXPLICIT
已经过时了。有几种情况它仍然有用。但你的问题最好像这样解决:
这是包含一些示例数据的表的简化模型。下次自己请add such an MCVE。
CREATE TABLE TStock (
id INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
Available_Quantity INT,
YourCode VARCHAR(100)
);
INSERT INTO TStock(Available_Quantity,YourCode) VALUES
(10,'Code Row 1')
,(20,'Code Row 2')
GO
- 查询将使用SELECT FOR XML PATH
两次
SELECT 'TEST' AS [@company_acro_name]
,'' AS [@company_mail_to]
,'N' AS [@hold_on_error]
,(
SELECT 'INVENTORY' AS [@recordtype]
,Available_Quantity AS [@quantity]
,YourCode AS [@supplierassortmentid]
FROM TStock
FOR XML PATH('product')
,TYPE) AS [*]
FOR XML PATH('inventory')
- 清理(小心真实数据!)
/*
GO
DROP TABLE TStock;
*/
结果
<inventory company_acro_name="TEST" company_mail_to="" hold_on_error="N">
<product recordtype="INVENTORY" quantity="10" supplierassortmentid="Code Row 1" />
<product recordtype="INVENTORY" quantity="20" supplierassortmentid="Code Row 2" />
</inventory>
关于 xml声明 <?xml blah?>
阅读以下链接:
https://stackoverflow.com/a/49768800/5089204(以及那里的相关答案)