0

我正在尝试制作 XML 以从 SQL Server 存储过程返回到 BizTalk,这将使我可以轻松地根据Attribution_TIN数据库中的数字对文件进行分批。

我需要遵循这种结构的 XML 格式的数据:

<ns1:Destination xmlns:ns1="XXX.OptOut_PCPPharmacy_SQL" Attribution_TIN="001">
    <Member PCP_ID="01" PCP_FullName="XXX" LastName="XXX" FirstName="XXX" Member_ID="XXX898XXX" Member_Gender="F" Member_Birth_Date="2011-11-08" Program_Name="xxx" Claim_Status="Paid Claim" Dispense_Date="2014-01-21" NDC_Number="000" Drug_Name="Ibuprofen 100 MG/5ML SUSP" Days_Supply="4" Dispensed_Quantity="100" PharmacyName="XXX" PrescribingName="XXX" />
    <Member PCP_ID="02" PCP_FullName="XXX" LastName="XXX" FirstName="XXX" Member_ID="XXX898XXX" Member_Gender="F" Member_Birth_Date="2011-11-08" Program_Name="xxx" Claim_Status="Paid Claim" Dispense_Date="2014-01-21" NDC_Number="000" Drug_Name="Ibuprofen 100 MG/5ML SUSP" Days_Supply="4" Dispensed_Quantity="100" PharmacyName="XXX" PrescribingName="XXX" />
</ns1:Destination>
<ns1:Destination xmlns:ns1="XXX.OptOut_PCPPharmacy_SQL" Attribution_TIN="002">
    <Member PCP_ID="01" PCP_FullName="XXX" LastName="XXX" FirstName="XXX" Member_ID="XXX898XXX" Member_Gender="F" Member_Birth_Date="2011-11-08" Program_Name="xxx" Claim_Status="Paid Claim" Dispense_Date="2014-01-21" NDC_Number="000" Drug_Name="Ibuprofen 100 MG/5ML SUSP" Days_Supply="4" Dispensed_Quantity="100" PharmacyName="XXX" PrescribingName="XXX" />
    <Member PCP_ID="02" PCP_FullName="XXX" LastName="XXX" FirstName="XXX" Member_ID="XXX898XXX" Member_Gender="F" Member_Birth_Date="2011-11-08" Program_Name="xxx" Claim_Status="Paid Claim" Dispense_Date="2014-01-21" NDC_Number="000" Drug_Name="Ibuprofen 100 MG/5ML SUSP" Days_Supply="4" Dispensed_Quantity="100" PharmacyName="XXX" PrescribingName="XXX" />
</ns1:Destination>

我有一个工作表,我从中提取数据并使用以下 FOR XML EXPLICIT 查询返回结果:

SELECT 1 AS Tag
      ,NULL AS Parent
      ,'XXX.OptOut_PCPPharmacy_SQL'  AS         [ns1:Destination!1!xmlns:ns1]
      ,PCP1.Attribution_TIN                                     AS [ns1:Destination!1!Attribution_TIN]
      ,NULL                                                     AS [Member!2!PCP_ID]
      ,NULL                                                     AS [Member!2!PCP_FullName]
      ,NULL                                                     AS [Member!2!LastName]
      ,NULL                                                     AS [Member!2!FirstName]
      ,NULL                                                     AS [Member!2!Member_ID]
      ,NULL                                                     AS [Member!2!Member_Gender]
      ,NULL                                                     AS [Member!2!Member_Birth_Date]
      ,NULL                                                     AS [Member!2!Program_Name]
      ,NULL                                                     AS [Member!2!Claim_Status]
      ,NULL                                                     AS [Member!2!Dispense_Date]
      ,NULL                                                     AS [Member!2!NDC_Number]
      ,NULL                                                     AS [Member!2!Drug_Name]
      ,NULL                                                     AS [Member!2!Days_Supply]
      ,NULL                                                     AS [Member!2!Dispensed_Quantity]
      ,NULL                                                     AS [Member!2!PharmacyName]
      ,NULL                                                     AS [Member!2!PrescribingName]
  FROM WorkTable AS PCP1
 UNION
SELECT 2 AS Tag
      ,1 AS Parent
      ,'XXX.OptOut_PCPPharmacy_SQL'                    
      ,PCP2.Attribution_TIN
      ,PCP2.PCP_ID                                         
      ,PCP2.PCP_Fullname
      ,PCP2.LastName
      ,PCP2.FirstName                          
      ,PCP2.Member_ID
      ,PCP2.Member_Gender       
      ,PCP2.Member_Birth_Date
      ,PCP2.[Program_Name]                                  
      ,PCP2.Claim_Status                                               
      ,PCP2.Dispense_Date                                  
      ,PCP2.NDC_Number                                     
      ,PCP2.Drug_Name                                  
      ,PCP2.Days_Supply                                    
      ,FLOOR(PCP2.Dispensed_Quantity)
      ,PCP2.PharmacyName                           
      ,PCP2.PrescribingName   
  FROM WorkTable AS PCP2 
    ORDER BY [ns1:Destination!1!xmlns:ns1]
         ,[ns1:Destination!1!Attribution_TIN]
FOR XML EXPLICIT;

这适用于小型结果集,但一旦结果变大,我就会收到以下错误:

Parent tag ID 1 is not among the open tags. FOR XML EXPLICIT requires parent tags to be opened first. Check the ordering of the result set.

我相信我知道问题所在,但不确定如何解决。当我在不使用 FOR XML EXPLICIT 的情况下对我的工作表运行上述查询时,它返回的结果如下:

Tag     Parent  ns1:Destination!1!xmlns:ns1             ns1:Destination!1!Attribution_TIN       Member!2!PCP_ID         Member!2!PCP_FullName   
2   1   XXX.OptOut_PCPPharmacy_SQL  010924601               XXX         XXX 
2   1   XXX.OptOut_PCPPharmacy_SQL  010924601               XXX         XXX

这就是 XML EXPLICIT 正确格式化 XML 结果的样子:

Tag     Parent  ns1:Destination!1!xmlns:ns1             ns1:Destination!1!Attribution_TIN       Member!2!PCP_ID         Member!2!PCP_FullName
1   NULL    XXX.OptOut_PCPPharmacy_SQL  010924601               NULL            NULL    
2   1   XXX.OptOut_PCPPharmacy_SQL  010924601               XXX         XXX 
2   1   XXX.OptOut_PCPPharmacy_SQL  010924601               XXX         XXX

我错过了什么?

我也试过使用FOR XML PATH无济于事

4

1 回答 1

1

在进行了更多挖掘之后,事实证明我试图FOR XML PATH完全错误地嵌套,这就是为什么它对我不起作用。我能够使用此查询获得所需的结果:

WITH XMLNAMESPACES (
'http://XXX.OptOut_PCPPharmacy_SQL' as ns1
)
SELECT  Q1.Attribution_Tin AS '@Attribution_TIN',
(SELECT PCP_ID                      AS '@PCP_ID'                                           
      ,PCP_Fullname                 AS '@PCP_FullName'
      ,LastName                     AS '@LastName'
      ,FirstName                    AS '@FirstName'                    
      ,Member_ID                    AS '@Member_ID'
      ,Member_Gender                AS '@Member_Gender'     
      ,Member_Birth_Date            AS '@Member_Birth_Date'
      ,[Program_Name]               AS '@Program_Name'                                
      ,Claim_Status                 AS '@Claim_Status'                                         
      ,Dispense_Date                AS '@Dispense_Date'                                
      ,NDC_Number                   AS '@NDC_Number'                                   
      ,Drug_Name                    AS '@Drug_Name'                                
      ,Days_Supply                  AS '@Days_Supply'                                  
      ,FLOOR(Dispensed_Quantity)    AS '@Dispensed_Quantity'
      ,PharmacyName                 AS '@PharmacyName'                 
      ,PrescribingName              AS '@PrescribingName'
  FROM WorkTable AS Q2
  WHERE Q2.Attribution_TIN = Q1.Attribution_TIN
  FOR XML PATH ('Member'), TYPE
)
FROM WorkTable AS Q1
GROUP BY Attribution_TIN
FOR XML PATH ('ns1:Destination');

现在我需要弄清楚如何让查询在更大的结果集(超过 100000 个)下运行得更快。现在运行 97k 条记录大约需要 20 分钟。

于 2014-02-02T17:07:57.733 回答