没有得到合适的结构

时间:2012-02-03 00:48:25

标签: sql sql-server tsql stored-procedures

我有这样的存储过程:

CREATE PROCEDURE [dbo].[Poll_EDI834_5010] 
AS 
BEGIN 
SELECT     Header.ST01,   
           Line.PER04__CommunicationNumber ,
           (SELECT  Member_Ref.REF01_ReferenceIdentificationQualifier
            FROM    EDI834_5010_2000_RefLoop Member_Ref  
            WHERE   Member_Ref.Id_Header = Header.BGN02__TransactionSetIdentifierCode             
            AND     Member_Ref.Id_Loop2000 = Line.Member_Id
            FOR XML AUTO, ELEMENTS, TYPE),    
           VLine.HD01_MaintenanceTypeCode,    
           (SELECT  ClaimDTP.DTP01__DateTimeQualifier 
            FROM    EDI834_5010_2300_DTPLoop ClaimDTP      
            WHERE   ClaimDTP.Id_Header = Header.BGN02__TransactionSetIdentifierCode      
            AND     ClaimDTP.Id_Loop2000 = Line.Member_Id      
            AND     ClaimDTP.Id_Loop2300 = VLine.Id_Claim      
            FOR XML AUTO, ELEMENTS, TYPE) 
FROM       EDI834_5010_Header   Header  
JOIN       EDI834_5010_2000     Line   ON Header.BGN02__TransactionSetIdentifierCode = Line.Header_Id 
JOIN       EDI834_5010_2300Loop VLine  ON Header.BGN02__TransactionSetIdentifierCode = VLine.Id_Header_Id andVLine.Id_Loop2000=Line.Member_Id
FOR XML AUTO, ELEMENTS 
END 
GO 

我得到的数据结构如下:

<Header>
     <Line>
        <MemberRef> </MemberRef>
        <MemberRef> </MemberRef>
        <VLine>
           <Claim_DTP>  </Claim_DTP>
           <Claim_DTP>  </Claim_DTP>
         </VLine>
     </Line>
 </Header>

我的问题是我希望数据看起来像这样:

  <Header>
    <Line>
       <MemberRef> </MemberRef>
       <MemberRef> </MemberRef>
       <VLine>
         <Claim_DTP>  </Claim_DTP>
         <Claim_DTP>  </Claim_DTP>
      </VLine>
       <VLine>
         <Claim_DTP>  </Claim_DTP>
         <Claim_DTP>  </Claim_DTP>
       </VLine>
   </Line>
</Header>

为什么我的VLine个节点不会重复?但是当我删除子查询Member_Ref时,我得到了适当的结构。但我需要那个子查询。有没有办法实现这种结构?

2 个答案:

答案 0 :(得分:2)

        CREATE PROCEDURE [dbo].[Poll_EDI834_5010]  
AS  
BEGIN  
        SELECT     Header.ST01,               
        Line.PER04__CommunicationNumber ,            
        (SELECT  Member_Ref.REF01_ReferenceIdentificationQualifier             
         FROM    EDI834_5010_2000_RefLoop Member_Ref               
         WHERE   Member_Ref.Id_Header = Header.BGN02__TransactionSetIdentifierCode                         
         AND     Member_Ref.Id_Loop2000 = Line.Member_Id             
         FOR XML AUTO, ELEMENTS, TYPE),                
        (SELECT  VLine.HD01_MaintenanceTypeCode,                
        (SELECT  ClaimDTP.DTP01__DateTimeQualifier                  
          FROM    EDI834_5010_2300_DTPLoop ClaimDTP                       
          WHERE   ClaimDTP.Id_Header = Header.BGN02__TransactionSetIdentifierCode                       
          AND     ClaimDTP.Id_Loop2000 = Line.Member_Id                 
          AND     ClaimDTP.Id_Loop2300 = VLine.Id_Claim                 
          FOR XML AUTO, ELEMENTS, TYPE) 
          FROM EDI834_5010_2300Loop VLine  
          Where  Header.BGN02__TransactionSetIdentifierCode = VLine.Id_Header_Id andVLine.Id_Loop2000=Line.Member_Id 

          FOR XML AUTO, ELEMENTS, TYPE) 
FROM       EDI834_5010_Header   Header 
JOIN       EDI834_5010_2000     Line   ON Header.BGN02__TransactionSetIdentifierCode = Line.Header_Id  
   FOR XML AUTO, ELEMENTS  
END  
GO  

答案 1 :(得分:1)

对此不确定......

CREATE PROCEDURE [dbo].[Poll_EDI834_5010] 
AS 
BEGIN 
SELECT     Header.ST01,   
           Line.PER04__CommunicationNumber ,
           (SELECT  Member_Ref.REF01_ReferenceIdentificationQualifier
            FROM    EDI834_5010_2000_RefLoop Member_Ref  
            WHERE   Member_Ref.Id_Header = Header.BGN02__TransactionSetIdentifierCode             
            AND     Member_Ref.Id_Loop2000 = Line.Member_Id
            FOR XML AUTO, ELEMENTS, TYPE),    
           (SELECT  VLine.HD01_MaintenanceTypeCode,
               (SELECT  ClaimDTP.DTP01__DateTimeQualifier 
                FROM    EDI834_5010_2300_DTPLoop ClaimDTP      
                WHERE   ClaimDTP.Id_Header = Header.BGN02__TransactionSetIdentifierCode      
                AND     ClaimDTP.Id_Loop2000 = Line.Member_Id
                AND     ClaimDTP.Id_Loop2300 = VLine.Id_Claim
                FOR XML AUTO, ELEMENTS, TYPE)
            FOR XML AUTO, ELEMENTS, TYPE)
FROM       EDI834_5010_Header   Header
JOIN       EDI834_5010_2000     Line   ON Header.BGN02__TransactionSetIdentifierCode = Line.Header_Id 
JOIN       EDI834_5010_2300Loop VLine  ON Header.BGN02__TransactionSetIdentifierCode = VLine.Id_Header_Id andVLine.Id_Loop2000=Line.Member_Id
FOR XML AUTO, ELEMENTS 
END 
GO