我有樣本數據問題與XML colunns在SQL服務器
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',
[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.
怎麼能我得到如下結果:
<xmlexecute>
<job>
<branch>
<FBEN>
<opcode>1</opcode>
<opcode>3</opcode>
<opcode>maj</opcode>
<1> 2534 </1>
</FBEN>
</branch>
</job>
</xmlexecute>
XML是相當嚴格的 - 你不能有它以數字開頭的元素名稱。有關類似問題,請參閱http://stackoverflow.com/questions/4756402/field-name-with-space-problem-in-case-of-for-xml-auto-output。 –
那麼我需要做的就是這個@JohnD – mohan111
Dude ...還有什麼可以解決的呢?使用任何不以數字字符開頭的其他字符。不是火箭科學。 –