2016-12-07 102 views
1

我有样本数据问题与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> 
+2

XML是相当严格的 - 你不能有它以数字开头的元素名称。有关类似问题,请参阅http://stackoverflow.com/questions/4756402/field-name-with-space-problem-in-case-of-for-xml-auto-output。 –

+0

那么我需要做的就是这个@JohnD – mohan111

+1

Dude ...还有什么可以解决的呢?使用任何不以数字字符开头的其他字符。不是火箭科学。 –

回答

0

试试这个:(这使正是你想要的结果):

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 replace((replace((replace((
select 
    users AS 'job/branch/FBEN/opcode', 
    ts as 'job/branch/FBEN/opcode1', 
    name as 'job/branch/FBEN/opcode2', 
    [1] AS 'job/branch/FBEN/zxdg1' 
from 
    @Table1 
where 
    ts = 3 
for xml path('xmlexecute'), elements),'opcode1','opcode')),'opcode2','opcode')),'zxdg1','1') 

Output:

<xmlexecute> 
     <job> 
      <branch> 
       <FBEN> 
       <opcode>1</opcode> 
       <opcode>3</opcode> 
       <opcode>maj</opcode> 
       <1>2534</1> 
      </FBEN> 
      </branch> 
     </job> 
</xmlexecute> 
+0

不!如果可能的话,一个**不应该在字符串级别对待XML **这会产生各种副作用,并可能导致绝对不可预知的错误。试想一下,“opcode1”这个词是 - 为什么 - 包含在其他地方... – Shnugo

+0

而btw:你的输出是一个字符串。它看起来像XML **,但它不是。您将无法使用此方法使用XML方法。尝试'CAST(YourResult AS XML)'...你会得到与OP一样的错误... – Shnugo

1

这是相同的答案给你other question - with explanation

这是不可能命名的元素<1>! XML不会允许这个! Look at "XML naming rules"

那么,你可以创建一个字符串,其中看起来像XML,但这是一个字符串,而不是XML。在大多数情况下,最好的选择是将此内容推入属性的价值......这取决于您的需求。

顺便说一句:我真的会重新考虑命名一列[1]。这将是 - 当然! - 在未来颈部疼痛...

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 
(
    select 
     'test' as [opcode] 
     ,'' 
     ,users AS [opcode] 
     ,'' 
     ,name as [opcode] 
     ,'' 
     ,[1] AS [opcode] --or any other (valid) name, start with an "@" for an attribute 
    from 
     @Table1 
    where 
     ts = 1 
    for xml path('FBEN'),ROOT('branch'),TYPE 
) AS job 
FOR XML PATH('xmlexecute') 
; 

结果

<xmlexecute> 
    <job> 
    <branch> 
     <FBEN> 
     <opcode>test</opcode> 
     <opcode>1</opcode> 
     <opcode>Raj</opcode> 
     <opcode>0</opcode> 
     </FBEN> 
    </branch> 
    </job> 
</xmlexecute>