使用SQL Server创建XML批处理文件

时间:2014-04-08 09:18:37

标签: sql xml sql-server-2008 batch-file

SELECT 
    (SELECT top 100 
         Student.LearnRefNumber AS StudentRefNumber,
         case 
            when PrevlearnRefNumber is null then ('') 
            when PrevlearnRefNumber is not null then PrevlearnRefNumber 
         end AS PrevlearnRefNumber,
         case 
            when PrevUKPRN is null then '' 
            when PrevUKPRN is not null then cast(PrevUKPRN as varchar) 
         end AS PrevUKPRN,
         cast(ULN as varchar) AS ULN,
         FamilyName AS FamilyName,
         GivenNames AS GivenNames,
         cast(DateOfBirth as varchar) AS DateOfBirth,
         cast(Ethnicity as varchar) AS Ethnicity,
         Sex AS Sex,
         NINumber AS NINumber,
         cast(PriorAttain as varchar) AS PriorAttain, 
         cast(FFRRHProb as varchar) AS FFRRHProb,
         case 
             when ALSCost is null then '' 
             when ALSCost is not null then cast(ALSCost as varchar)
         end AS ALSCost,
         cast(PLHours as varchar) AS PLHours,
         case 
            when PERPours is null then '' 
            when PERPHours is not null then cast(PERPHours as varchar) 
         end AS PERPHours,
         cast(Aest as varchar) AS Aest,
         cast(TocType as varchar) AS TocType,
         case 
            when AddLine1 is null then '' 
            when AddLine1 is not null then AddLine1 
         end AS AddLine1,
         case 
            when AddLine2 is null then '' 
            when AddLine2 is not null then AddLine2 
         end AS AddLine2, 
         case 
            when AddLine3 is null then '' 
            when AddLine3 is not null then AddLine3 
         end AS AddLine3,
         case when AddLine4 is null then '' when AddLine4 is not null then AddLine4 end AS AddLine4,
         case when PostCode is null then '' when PostCode is not null then PostCode end AS  PostCode,
         case when Email is null then '' when Email is not null then Email end AS Email,
         case when TelNumber is null then '' when TelNumber is not null then TelNumber end AS TelNumber, 
         CPType AS CPType,
         cast(CPCode as varchar) AS CPCode,
         FFRRTYPE AS FFRRTYPE,
         CAST(FFRRCode AS VARCHAR) AS FFRRCode,
         StudentTamType AS StudentTamType,
         CAST(StudentTamCode AS VARCHAR) AS StudentTamCode   
      FROM 
         Student 
      LEFT OUTER JOIN 
         StudentContact ON Student.StudentRefNumber = StudentContact.StudentRefNumber
      LEFT OUTER JOIN 
         StudentPreference ON Student.StudentRefNumber = StudentPreference.StudentRefNumber
      LEFT OUTER JOIN 
         HealthIssues ON Student.StudentRefNumber = HealthIssue.StudentRefNumber
      LEFT OUTER JOIN 
         StudentTAM ON Student.StudentRefNumber = StudentTAM.StudentRefNumber
      ORDER BY 
         Student.StudentRefNumber
      FOR
         XML PATH ('Student'), TYPE  
)
FOR XML PATH(''), ROOT ('xxxxxxx')
GO

我更改了表名和列,所以如果有未命中匹配,请忽略。

我基本上需要创建一个xml批处理文件,其中包含您在代码中看到的数据。

Aest下的所有内容都需要位于XML路径Student的其他子关系之下,这是第一个需要修复的问题。

接下来的事情是,所有内容都需要链接到当前的StudentRefNumber,因为它全部属于一个from子句。但通过这样做,我没有将数据分成儿童关系。

另一个问题是数据的存储方式更适合于xml结构而不是关系数据库结构。因此,所有其他表与学生之间存在一对多的关系。这导致大量数据分离出来。

我想要的是学生联系xml路径,该路径是学生的父路径,其中所有不同的学生联系信息将是一对多,因此每个学生路径将有多个设置。

0 个答案:

没有答案