2016-12-07 99 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>