2013-12-20 175 views
2

我有一個爲我生成XML輸出的存儲過程。TSQL格式查詢輸出

的SP看起來是這樣的:

ALTER PROCEDURE [dbo].[InternFetchProf] 
@positionID INT='' 
AS 
BEGIN 
SET NOCOUNT ON; 
BEGIN 
    SELECT A.[id], 
      A.[positionTitle], 
      (SELECT B.[id], 
         B.[question], 
         B.[order], 
         (SELECT D.[optionName], 
           D.[order] 
         FROM  internshipProfOptions AS D 
         WHERE questionID = B.[id] 
         ORDER BY D.[order] ASC 
         FOR  XML PATH ('optionsBlock'), TYPE, ELEMENTS, ROOT ('options')) 
       FROM  internshipProfQuestions AS B 
       WHERE positionID = A.[id] 
       ORDER BY B.[order] ASC 
       FOR  XML PATH ('questionBlock'), TYPE, ELEMENTS, ROOT ('questions')) 
    FROM  internships AS a 
    WHERE A.[id] = @positionID 
    ORDER BY closeDate ASC 
    FOR  XML PATH ('positions'), TYPE, ELEMENTS, ROOT ('root'); 
END 
END 

這給我的

     <root> 
       <positions> 
       <id>1</id> 
       <positionTitle>APS Team</positionTitle> 
       <questions> 
        <questionBlock> 
        <id>1</id> 
        <question>Whats your fav color</question> 
        <order>1</order> 
        <options> 
         <optionsBlock> 
         <optionName>VBA</optionName> 
         <order>1</order> 
         </optionsBlock> 
         <optionsBlock> 
         <optionName>JavaScript</optionName> 
         <order>2</order> 
         </optionsBlock> 
         <optionsBlock> 
         <optionName>HTML</optionName> 
         <order>3</order> 
         </optionsBlock> 
        </options> 
        </questionBlock> 
        <questionBlock> 
        <id>2</id> 
        <question>Whos your daddy?</question> 
        <order>2</order> 
        <options> 
         <optionsBlock> 
         <optionName>PHP</optionName> 
         <order>1</order> 
         </optionsBlock> 
         <optionsBlock> 
         <optionName>Perl</optionName> 
         <order>2</order> 
         </optionsBlock> 
        </options> 
        </questionBlock> 
       </questions> 
       </positions> 
      </root> 

我有一個額外的SELECT語句這裏的XML輸出:

SELECT C.[groupName], 
          C.[order], 
         FROM internshipProfGroups AS C 
         WHERE questionID = B.[id] 
         FOR XML PATH ('groupBlock'), TYPE, ELEMENTS, ROOT ('groups') 

我正努力在「QuestionBlock」的根目錄下生成XML,如下所示:

  <questionBlock> 
      ... 

      <groups> 
       <groupBlock> 
        <groupName>Testing</groupName> 
       </groupBlock> 
       <groupBlock> 
        <groupName>Another Test</groupName> 
       </groupBlock> 
      </groups> 

      ... 
      </questionBlock> 

我很努力地在正確的領域得到這樣的陳述。對我可能錯過的任何幫助?

回答

1

如果我深知你需要插入第二個查詢在相同的水平「SELECT D. [OPTIONNAME]」(還我認爲你必須在代碼中的一些錯誤):

SELECT 
    A.[id], 
    A.[positionTitle], 
    (SELECT B.[id], 
      B.[question], 
      B.[ord], 
      (SELECT D.[optionName], 
         D.[ord] 
      FROM  internshipProfOptions AS D 
      WHERE questionID = B.[id] 
      ORDER BY D.[ord] ASC 
      FOR  XML PATH ('optionsBlock'), TYPE, ELEMENTS, ROOT ('options')), 
      (SELECT C.[groupName], 
         C.[ord] 
      FROM internshipProfGroups AS C 
      WHERE questionID = B.[id] 
      FOR XML PATH ('groupBlock'), TYPE, ELEMENTS, ROOT ('groups')) 
    FROM  internshipProfQuestions AS B 
    WHERE positionID = A.[id] 
    ORDER BY B.[ord] ASC 
    FOR  XML PATH ('questionBlock'), TYPE, ELEMENTS, ROOT ('questions')) 
FROM internships A 
WHERE A.[id] = 3 
ORDER BY closeDate ASC 
FOR  XML PATH ('positions'), TYPE, ELEMENTS, ROOT ('root'); 

WORKING SAMPLE

給出如下結果:

<?xml version="1.0"?> 
<root> 
    <positions> 
     <id>3</id> 
     <positionTitle>Trimepost</positionTitle> 
     <questions> 
      <questionBlock> 
       <id>2</id> 
       <question>What?</question> 
       <ord>1</ord> 
       <options> 
        <optionsBlock> 
         <optionName>Opt 2</optionName> 
         <ord>3</ord> 
        </optionsBlock> 
       </options> 
       <groups> 
        <groupBlock> 
         <groupName>ALPHA</groupName> 
         <ord>1</ord> 
        </groupBlock> 
        <groupBlock> 
         <groupName>BETA</groupName> 
         <ord>2</ord> 
        </groupBlock> 
       </groups> 
      </questionBlock> 
     </questions> 
    </positions> 
</root>