Sql server 如何使用模式验证SQL表数据

Sql server 如何使用模式验证SQL表数据,sql-server,xml,validation,sql-server-2012,schema,Sql Server,Xml,Validation,Sql Server 2012,Schema,我的理解力很差,所以真的需要一个地方开始寻找信息 我有4个SQL数据表。我想将它们全部输出到一个XML文件中 我有一个XML模式要处理,但不知道如何使用它,也不知道应该在哪里使用它 长期目标是拥有30个移交表,我们可以对这些表运行验证脚本,然后使用给定的XML方案生成1个XML文件以供提交 我是一个新手在这里和学习,因为我去,所以任何建议在哪里寻找将不胜感激 示例XML数据 <?xml version="1.0" encoding="UTF-8"?> <!--Sample XM

我的理解力很差,所以真的需要一个地方开始寻找信息

我有4个SQL数据表。我想将它们全部输出到一个XML文件中

我有一个XML模式要处理,但不知道如何使用它,也不知道应该在哪里使用它

长期目标是拥有30个移交表,我们可以对这些表运行验证脚本,然后使用给定的XML方案生成1个XML文件以供提交

我是一个新手在这里和学习,因为我去,所以任何建议在哪里寻找将不胜感激

示例XML数据

<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSpy v2013 rel. 2 sp2 (http://www.altova.com)-->
-<MSDS:MSDS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:MSDS="http://www.datadictionary.nhs.uk/messages/MSDS-v1-0" xsi:schemaLocation="http://www.datadictionary.nhs.uk/messages/MSDS-v1-0 ../Schemas/MSDSMSDS_XMLSchema-v1-0.xsd">

-<MATHDRHeader>

<Version>1.0</Version>
<OrgCodeProv>5BC</OrgCodeProv>
<OrgCodeSubmitter>YEA</OrgCodeSubmitter>
<RPStartDate>2013-01-01</RPStartDate>
<RPEndDate>2013-03-12</RPEndDate>
<FileCreationDateTime>2013-03-13T13:00:27</FileCreationDateTime>
<RecordCount>1</RecordCount>


-<MAT001MothersDemographics>

<LocalPatientIdMother>112552254</LocalPatientIdMother>
<OrgCodeLocalPatientIdMother>5BC</OrgCodeLocalPatientIdMother>
<OrgCodeRes>5BC</OrgCodeRes>
<NHSNumberMother>1111111111</NHSNumberMother>
<NHSNumberStatusMother>01</NHSNumberStatusMother>
<PersonBirthDateMother>1982-01-05</PersonBirthDateMother>
<Postcode>LS1 4HY</Postcode>
<EthnicCategoryMother>99</EthnicCategoryMother>
<PersonDeathDateTimeMother>1900-01-01T00:00:00</PersonDeathDateTimeMother>


-<MAT003GPPracticeRegistration>

<LocalPatientIdMother>112552254</LocalPatientIdMother>
<OrgCodeGMPMother>4RT</OrgCodeGMPMother>
<StartDateGMPRegistration>2012-01-06</StartDateGMPRegistration>
<EndDateGMPRegistration>1900-01-01</EndDateGMPRegistration>
<OrgCodeCommissioner>6TY</OrgCodeCommissioner>
</MAT003GPPracticeRegistration>


-<MAT101BookingAppointmentDetails>

<AntenatalAppDate>2013-03-01</AntenatalAppDate>
<LocalPatientIdMother>112552254</LocalPatientIdMother>
<EDDAgreed>2013-05-01</EDDAgreed>
<EDDMethodAgreed>01</EDDMethodAgreed>
<PregnancyFirstContactDate>2013-11-11</PregnancyFirstContactDate>
<PregnancyFirstContactCareProfessionalType>060</PregnancyFirstContactCareProfessionalType>
<LastMenstrualPeriodDate>2012-10-01</LastMenstrualPeriodDate>
<PhysicalDisabilityStatusIndMother>Y</PhysicalDisabilityStatusIndMother>
<FirstLanguageEnglishIndMother>Y</FirstLanguageEnglishIndMother>
<EmploymentStatusMother>04</EmploymentStatusMother>
<SupportStatusMother>Y</SupportStatusMother>
<EmploymentStatusPartner>06</EmploymentStatusPartner>
<PreviousCaesareanSections>0</PreviousCaesareanSections>
<PreviousLiveBirths>0</PreviousLiveBirths>
<PreviousStillBirths>0</PreviousStillBirths>
<PreviousLossesLessThan24Weeks>0</PreviousLossesLessThan24Weeks>
<SubstanceUseStatus>01</SubstanceUseStatus>
<SmokingStatus>03</SmokingStatus>
<CigarettesPerDay>0</CigarettesPerDay>
<AlcoholUnitsPerWeek>0</AlcoholUnitsPerWeek>
<FolicAcidSupplement>03</FolicAcidSupplement>
<MHPredictionDetectionIndMother>N</MHPredictionDetectionIndMother>
<PersonWeight>75.0</PersonWeight>
<PersonHeight>1.45</PersonHeight>
<ComplexSocialFactorsInd>N</ComplexSocialFactorsInd>

</MAT101BookingAppointmentDetails>

</MATHDRHeader>

</MSDS:MSDS>

-
-
1
5BC
赞成
2013-01-01
2013-03-12
2013-03-13T13:00:27
1.
-
112552254
5BC
5BC
1111111111
01
1982-01-05
LS1 4HY
99
1900-01-01T00:00:00
-
112552254
4RT
2012-01-06
1900-01-01
6TY
-
2013-03-01
112552254
2013-05-01
01
2013-11-11
060
2012-10-01
Y
Y
04
Y
06
0
0
0
0
01
03
0
0
03
N
75
1.45
N
样本XDS

 <xs:complexType name="MSDSMAT001MothersDemographicsType">
      <xs:sequence>
         <xs:element name="LocalPatientIdMother" type="ST" minOccurs="0" maxOccurs="1">
            <xs:annotation>
               <xs:appinfo>LOCAL PATIENT IDENTIFIER (MOTHER)</xs:appinfo>
            </xs:annotation>
         </xs:element>
         <xs:element name="OrgCodeLocalPatientIdMother" type="ST" minOccurs="0" maxOccurs="1">
            <xs:annotation>
               <xs:appinfo>ORGANISATION CODE (LOCAL PATIENT IDENTIFIER (MOTHER))</xs:appinfo>
            </xs:annotation>
         </xs:element>
         <xs:element name="OrgCodeRes" type="ST" minOccurs="0" maxOccurs="1">
            <xs:annotation>
               <xs:appinfo>ORGANISATION CODE (RESIDENCE RESPONSIBILITY)</xs:appinfo>
            </xs:annotation>
         </xs:element>
         <xs:element name="NHSNumberMother" type="ST" minOccurs="0" maxOccurs="1">
            <xs:annotation>
               <xs:appinfo>NHS NUMBER (MOTHER)</xs:appinfo>
            </xs:annotation>
         </xs:element>
         <xs:element name="NHSNumberStatusMother" type="ST" minOccurs="0" maxOccurs="1">
            <xs:annotation>
               <xs:appinfo>NHS NUMBER STATUS INDICATOR CODE (MOTHER)</xs:appinfo>
            </xs:annotation>
         </xs:element>
         <xs:element name="PersonBirthDateMother" type="ST" minOccurs="0" maxOccurs="1">
            <xs:annotation>
               <xs:appinfo>PERSON BIRTH DATE (MOTHER)</xs:appinfo>
            </xs:annotation>
         </xs:element>
         <xs:element name="Postcode" type="ST" minOccurs="0" maxOccurs="1">
            <xs:annotation>
               <xs:appinfo>POSTCODE OF USUAL ADDRESS (MOTHER)</xs:appinfo>
            </xs:annotation>
         </xs:element>
         <xs:element name="EthnicCategoryMother" type="ST" minOccurs="0" maxOccurs="1">
            <xs:annotation>
               <xs:appinfo>ETHNIC CATEGORY (MOTHER)</xs:appinfo>
            </xs:annotation>
         </xs:element>
         <xs:element name="PersonDeathDateTimeMother" type="ST" minOccurs="0" maxOccurs="1">
            <xs:annotation>
               <xs:appinfo>PERSON DEATH DATE TIME (MOTHER)</xs:appinfo>
            </xs:annotation>
         </xs:element>
         <xs:element name="MAT003GPPracticeRegistration" type="MSDSMAT003GPPracticeRegistrationType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
         <xs:element name="MAT101BookingAppointmentDetails"
                     type="MSDSMAT101BookingAppointmentDetailsType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
         <xs:element name="MAT112DatingScanProcedure" type="MSDSMAT112DatingScanProcedureType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
         <xs:element name="MAT201BloodGroupRhesusTest" type="MSDSMAT201BloodGroupRhesusTestType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
         <xs:element name="MAT203RubellaSusceptibilityTest"
                     type="MSDSMAT203RubellaSusceptibilityTestType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
         <xs:element name="MAT205HepatitisBScreeningTest"
                     type="MSDSMAT205HepatitisBScreeningTestType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
         <xs:element name="MAT210AsymptomaticBacteriuriaScreeningOffer"
                     type="MSDSMAT210AsymptomaticBacteriuriaScreeningOfferType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
         <xs:element name="MAT211HaemoglobinopathyScreeningTest"
                     type="MSDSMAT211HaemoglobinopathyScreeningTestType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
         <xs:element name="MAT301MaternityCarePlan" type="MSDSMAT301MaternityCarePlanType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
         <xs:element name="MAT303DownsSyndromeScreeningTest"
                     type="MSDSMAT303DownsSyndromeScreeningTestType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
         <xs:element name="MAT305FetalAnomalyScreeningTest"
                     type="MSDSMAT305FetalAnomalyScreeningTestType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
         <xs:element name="MAT306AntenatalAppointment" type="MSDSMAT306AntenatalAppointmentType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
         <xs:element name="MAT307MedicalDiag" type="MSDSMAT307MedicalDiagType" minOccurs="0"
                     maxOccurs="unbounded"/>
         <xs:element name="MAT309MaternityObstetricDiag" type="MSDSMAT309MaternityObstetricDiagType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
         <xs:element name="MAT310AntenatalAdmission" type="MSDSMAT310AntenatalAdmissionType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
         <xs:element name="MAT404LabourAndDelivery" type="MSDSMAT404LabourAndDeliveryType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
         <xs:element name="MAT408MCI" type="MSDSMAT408MCIType" minOccurs="0" maxOccurs="unbounded"/>
         <xs:element name="MAT501FetusOutcome" type="MSDSMAT501FetusOutcomeType" minOccurs="0"
                     maxOccurs="unbounded"/>
         <xs:element name="MAT502BabysDemographicsAndBirthDetails"
                     type="MSDSMAT502BabysDemographicsAndBirthDetailsType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
         <xs:element name="MAT602PostpartumDischarge" type="MSDSMAT602PostpartumDischargeType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
         <xs:element name="MAT603PostpartumReadmission" type="MSDSMAT603PostpartumReadmissionType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
      </xs:sequence>
   </xs:complexType>

本地患者标识符(母亲)
组织代码(本地患者标识符(母亲))
组织机构代码(居住责任)
NHS号码(母亲)
NHS编号状态指示器代码(母亲)
个人出生日期(母亲)
通常地址(母亲)的邮政编码
种族类别(母亲)
人死亡日期时间(母亲)
看来这一条太难了,因为在任何网站上都找不到回复

嗯,我不能让这件事不受挑战地过去

太难了必须讨论。。。一个问题是,架构以及您提供的数据不完整/无效。。。在试图缩短提供的样本数据时(这太棒了!)您创建了无效的样本数据。。。 我需要投入大量的时间,首先了解你真正想要的是什么,然后建立一个模拟项目

而现在我们正处于太难的中心:我没有时间做这个。。。这是你应该提供的东西。最好的是一个可复制的独立项目。请阅读和

SQL Server不支持内置架构检查或与架构相关的自动导出。因此,我在第一次评论中告诉您,T-SQL可能是错误的工具

您没有回答问题是否有其他可用的工具/语言

这段代码是一个非常简单的C代码。它将从模式中创建数据集的内部结构,然后将给定的XML加载到此数据集中。当XML不兼容时,这将失败。这可能是对您的架构检查

//上述内容在一个
try catch
中,包装在一个函数中,可能就足够检查模式了
//下面的代码将把数据读入一个字符串以检查是否成功(完全未测试…)

最后(正如我在评论中指出的,如果我理解正确,您根本不需要模式),我将使用
FOR XML PATH
创建XML,这正是您需要的方式。这与复杂的访问遗留代码所采用的方法大致相同:只需创建正确的XML即可。访问解决方案是否反映了任何模式?可能不是。。。明白我的意思吗

更新只是为了给你一个想法: --一些包含微小数据的小表格。更多的列只是更多的相同

--我创建的Inner XML没有名称空间,因为它们似乎只出现在最外层的节点中

DECLARE @MotherID BIGINT=222222;
DECLARE @innerXML XML= --without namespaces
(
SELECT '1.0' AS [MVersion]
      ,'5BC' AS [OrgCodeProv]
      ,GETDATE() AS [FileCreationDateTime]
      ,(
        SELECT ID AS LocalPatientIdMother
              ,Code AS OrgCodeLocalPatientIdMother
        FROM MAT001MothersDemographics
        WHERE ID=@MotherID
        FOR XML PATH('MAT001MothersDemographics'),TYPE
       )
      ,(
        SELECT MotherID AS LocalPatientIdMother
              ,StartDate AS StartDateGMPRegistration
        FROM MAT003GPPracticeRegistration
        WHERE MotherID=@MotherID
        FOR XML PATH('MAT003GPPracticeRegistration'),TYPE
       )
FOR XML PATH('MATHDRHeader')
);
--这将使用
节点包装先前创建的XML

WITH XMLNAMESPACES('http://www.w3.org/2001/XMLSchema-instance' AS xsi 
                  ,'http://www.datadictionary.nhs.uk/messages/MSDS-v1-0' AS MSDS
                  ,'http://www.datadictionary.nhs.uk/messages/MSDS-v1-0 ../Schemas/MSDSMSDS_XMLSchema-v1-0.xsd' AS schemaLocation)
SELECT @innerXML
FOR XML PATH('MSDS:MSDS');
--清理

这一小部分的结果与您所需要的非常接近:

    <MSDS:MSDS xmlns:schemaLocation="http://www.datadictionary.nhs.uk/messages/MSDS-v1-0 ../Schemas/MSDSMSDS_XMLSchema-v1-0.xsd" xmlns:MSDS="http://www.datadictionary.nhs.uk/messages/MSDS-v1-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <MATHDRHeader>
        <MVersion>1.0</MVersion>
        <OrgCodeProv>5BC</OrgCodeProv>
        <FileCreationDateTime>2017-04-21T10:45:57.590</FileCreationDateTime>
        <MAT001MothersDemographics>
          <LocalPatientIdMother>222222</LocalPatientIdMother>
          <OrgCodeLocalPatientIdMother>2B2</OrgCodeLocalPatientIdMother>
        </MAT001MothersDemographics>
        <MAT003GPPracticeRegistration>
          <LocalPatientIdMother>222222</LocalPatientIdMother>
          <StartDateGMPRegistration>2002-02-02</StartDateGMPRegistration>
        </MAT003GPPracticeRegistration>
      </MATHDRHeader>
    </MSDS:MSDS>

1
5BC
2017-04-21T10:45:57.590
222222
2B2
222222
2002-02-02

请提供更多详细信息(模式、简化样本、预期输出),可能会发现T-SQL是错误的语言。。。是否有其他可用的工具/语言?架构长度为77000个字符?我听说SSIS是更好的方法。使用
.Net
,您可以-例如-从有效架构创建
数据集
,然后尝试将数据加载到此架构中。。。在SQL Server中,对模式数据验证的支持相当差。我担心具有的数据源于SQL表,正如我所说,将结果集输出到XML文件的目标我所理解的:有些数据库表包含数据,您希望将它们导出为XML(文件)。当你使用一个模式来实现这个简单的过程时,你希望有某种自动性。。。到目前为止正确吗?每个表一个XML还是一个大的包含所有内容的怪物?
CREATE TABLE MAT001MothersDemographics(ID BIGINT, Code VARCHAR(100));
INSERT INTO MAT001MothersDemographics VALUES(1111111,'1A1')
                                           ,(222222,'2B2');

CREATE TABLE MAT003GPPracticeRegistration(ID BIGINT,MotherID BIGINT,StartDate Date);
INSERT INTO MAT003GPPracticeRegistration VALUES(1,111111,{d'2001-01-01'})
                                              ,(2,222222,{d'2002-02-02'});
DECLARE @MotherID BIGINT=222222;
DECLARE @innerXML XML= --without namespaces
(
SELECT '1.0' AS [MVersion]
      ,'5BC' AS [OrgCodeProv]
      ,GETDATE() AS [FileCreationDateTime]
      ,(
        SELECT ID AS LocalPatientIdMother
              ,Code AS OrgCodeLocalPatientIdMother
        FROM MAT001MothersDemographics
        WHERE ID=@MotherID
        FOR XML PATH('MAT001MothersDemographics'),TYPE
       )
      ,(
        SELECT MotherID AS LocalPatientIdMother
              ,StartDate AS StartDateGMPRegistration
        FROM MAT003GPPracticeRegistration
        WHERE MotherID=@MotherID
        FOR XML PATH('MAT003GPPracticeRegistration'),TYPE
       )
FOR XML PATH('MATHDRHeader')
);
WITH XMLNAMESPACES('http://www.w3.org/2001/XMLSchema-instance' AS xsi 
                  ,'http://www.datadictionary.nhs.uk/messages/MSDS-v1-0' AS MSDS
                  ,'http://www.datadictionary.nhs.uk/messages/MSDS-v1-0 ../Schemas/MSDSMSDS_XMLSchema-v1-0.xsd' AS schemaLocation)
SELECT @innerXML
FOR XML PATH('MSDS:MSDS');
GO
USE master;
GO
DROP DATABASE TestDB;
GO
    <MSDS:MSDS xmlns:schemaLocation="http://www.datadictionary.nhs.uk/messages/MSDS-v1-0 ../Schemas/MSDSMSDS_XMLSchema-v1-0.xsd" xmlns:MSDS="http://www.datadictionary.nhs.uk/messages/MSDS-v1-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <MATHDRHeader>
        <MVersion>1.0</MVersion>
        <OrgCodeProv>5BC</OrgCodeProv>
        <FileCreationDateTime>2017-04-21T10:45:57.590</FileCreationDateTime>
        <MAT001MothersDemographics>
          <LocalPatientIdMother>222222</LocalPatientIdMother>
          <OrgCodeLocalPatientIdMother>2B2</OrgCodeLocalPatientIdMother>
        </MAT001MothersDemographics>
        <MAT003GPPracticeRegistration>
          <LocalPatientIdMother>222222</LocalPatientIdMother>
          <StartDateGMPRegistration>2002-02-02</StartDateGMPRegistration>
        </MAT003GPPracticeRegistration>
      </MATHDRHeader>
    </MSDS:MSDS>