2014-06-27 80 views
0

我從表中選擇數據作爲xml。我用這個XML路徑,我已經成功。但在xml中,我還需要添加一個名爲return的節作爲稍後使用的虛擬節點。我也有選擇查詢來獲得返回部分爲XML。但現在我需要結合這兩個選擇查詢來獲得單個XML。在xml中選擇聯合查詢

我的代碼

SELECT 
CAST(IDNO AS VARCHAR(8)) AS [IDNO]   
,CAST(Code AS VARCHAR(4)) AS [CODE]   
,CAST(Amt AS VARCHAR(18)) AS [Amt]   
FROM tbl1 
FOR XML PATH('ITEM'), ROOT('Table1'), TYPE 


SELECT 
'' AS INAME 
,'' AS UNQ_ID 
,'' AS STATUS 
FOR XML PATH('IT_RETURN') 

,請告訴我。

編輯:我的預期成果是

<Table1> 
    <ITEM> 
    <IDNO>0283883</IDNO> 
    <CODE>ABC</CODE> 
    <Amt>20</Amt> 
    </ITEM> 
    <ITEM> 
    <IDNO>0374747</IDNO> 
    <CODE>DHF</CODE> 
    <Amt>10</Amt> 
    </ITEM> 
</Table1> 
<IT_RETURN> 
    <INAME></INAME> 
    <UNQ_ID></UNQ_ID> 
    <STATUS></STATUS> 
</IT_RETURN> 

回答

0

下面將做的伎倆爲您提供:

DECLARE @tbl1 TABLE (
    IDNO VARCHAR(8), 
    Code VARCHAR(4), 
    Amt VARCHAR(18) 
) 
INSERT INTO 
    @tbl1 
VALUES 
    ('0283883', 'ABC', '20'), 
    ('0374747', 'DHF', '10') 

SELECT (
    SELECT 
     CAST(IDNO AS VARCHAR(8)) AS [IDNO], 
     CAST(Code AS VARCHAR(4)) AS [CODE], 
     CAST(Amt AS VARCHAR(18)) AS [Amt]   
    FROM 
     @tbl1 
    FOR XML PATH('ITEM'), ROOT('Table1'), TYPE 
    ), (
    SELECT 
     '' AS INAME 
     ,'' AS UNQ_ID 
     ,'' AS STATUS 
    FOR XML PATH('IT_RETURN'), TYPE 
) FOR XML PATH('') 
+0

感謝朋友。它的工作 – Akhil

+0

我很高興它幫助=) –

0

使用下面的查詢:

SELECT IDNO 
,CODE 
,Amt 
FROM 
(
    SELECT 
    CAST('56' AS VARCHAR(8)) AS [IDNO]   
    ,CAST('44' AS VARCHAR(4)) AS [CODE]   
    ,CAST('33434' AS VARCHAR(18)) AS [Amt]   
    FROM tbl1 

    UNION ALL 

    SELECT 
    '' --AS INAME 
    ,'' --AS UNQ_ID 
    ,'' --AS STATUS 
) t 
FOR XML PATH('ITEM'), ROOT('Table1'), TYPE