我有樣本數據如何格式化XML代碼時,數據在一條線來
DECLARE @Table1 TABLE (users int, ts int, name varchar(10), [1] int);
INSERT INTO @Table1 (users, ts, name, [1])
VALUES (1, 1, 'Raj', 0),
(1, 3, 'maj', 2534),
(1, 10, 'yes', 1458);
腳本
select
'test' as 'job/branch/FBEN/opcode',
users AS 'job/branch/FBEN/opcode',
name as 'job/branch/FBEN/opcode'
from
@Table1
where
ts = 1
for xml path('xmlexecute'), elements;
輸出:
<xmlexecute>
<job>
<branch>
<FBEN>
<opcode>test1Raj</opcode>
</FBEN>
</branch>
</job>
</xmlexecute>
我怎樣才能得到這樣代替?
<xmlexecute>
<job>
<branch>
<FBEN>
<opcode>test1Raj</opcode>
<opcode>1</opcode>
<opcode>Raj</opcode>
</FBEN>
</branch>
</job>
</xmlexecute>
如果我嘗試添加
select
'test' as 'job/branch/FBEN/opcode',
users AS 'job/branch/FBEN/opcode',
name as 'job/branch/FBEN/opcode',
[1] AS 'job/branch/FBEN/opcode/1'
from
@Table1
where
ts = 3
for xml path('xmlexecute'), elements;
我得到一個錯誤:
Msg 6850, Level 16, State 1, Line 14
Column name 'job/branch/FBEN/opcode/1' contains an invalid XML identifier as required by FOR XML; '1'(0x0031) is the first character at fault.
檢查這個網址:http://stackoverflow.com/questions/34938591/for-xml-structure-with-two-parent-nodes-and-multiple-child-nodes –