Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
动态XML到SQL-sqlserver_Sql_Sql Server_Xml_Tsql_Xsd - Fatal编程技术网

动态XML到SQL-sqlserver

动态XML到SQL-sqlserver,sql,sql-server,xml,tsql,xsd,Sql,Sql Server,Xml,Tsql,Xsd,我有一个非常复杂的XML,我只是尝试编写动态SQL查询,将数据从XML插入SQL Server数据库 下面是XML的XSD:: <xs:element name="Department"> <xs:complexType> <xs:all> <xs:element name="DeptID" type="xs:string" minOccurs="0" maxOccurs="1" />

我有一个非常复杂的XML,我只是尝试编写动态SQL查询,将数据从XML插入SQL Server数据库

下面是XML的XSD::

<xs:element name="Department">
    <xs:complexType>
        <xs:all>
            <xs:element name="DeptID" type="xs:string" minOccurs="0" maxOccurs="1" />
            <xs:element name="DepRefNum" type="xs:string" minOccurs="0" maxOccurs="1" />
            <xs:element ref="date"/>            
            <xs:element ref="Employees" minOccurs="0" maxOccurs="1"/>
        </xs:all>
    </xs:complexType>
</xs:element>

<xs:element name="Employees">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="Employee" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="Employee">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Name" minOccurs="0" maxOccurs="1"/>
            <xs:element name="Code" type="xs:string" minOccurs="0" maxOccurs="1"/>
            <xs:element ref="Date" minOccurs="0" maxOccurs="1"/>            
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="Date" type="TypeStringWithTypeAttribute"/>

<xs:complexType name="TypeStringWithTypeAttribute">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute name="type" type="xs:string"/>
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>
我有两个表,我正在尝试以下面的格式获取数据,其中
Dept\u ID
来自XML,但是
EMP\u ID
是一个标识,我希望
Dept\u ID
用作外键,这不是问题,只是为了信息:

部门

Dept_ID     Dept_Ref_Num    Create_Date_Type    Create_Date
---------------------------------------------------------------
D101                        Create              01/1/2017 12:30
Emp_ID   Dept_ID    Emp_Name    Emp_Code    Date_Type   Date
-------------------------------------------------------------------------
   1     D101       Jason       J111        DOJ         06/18/2018 14:36
   2     D101       Roy         R111        DOJ         06/18/2018 14:36
员工

Dept_ID     Dept_Ref_Num    Create_Date_Type    Create_Date
---------------------------------------------------------------
D101                        Create              01/1/2017 12:30
Emp_ID   Dept_ID    Emp_Name    Emp_Code    Date_Type   Date
-------------------------------------------------------------------------
   1     D101       Jason       J111        DOJ         06/18/2018 14:36
   2     D101       Roy         R111        DOJ         06/18/2018 14:36

我面临的问题是因为它是动态xml,我不知道实时xml中会有多少员工,也就是说,因为employees下的employees是无限的。

更正了xml上的close标记

也许是这样的:

DECLARE @x XML='<Department>
   <DeptID>D101</DeptID>
   <DepRefNum></DepRefNum>
   <Date type="Create">01/1/2017 12:30</Date>
   <Employees>
         <Employee>
             <Name>Jason</Name>
             <Code>J111</Code>
             <Date type="DOJ">06/18/2018 14:36</Date>
         </Employee>

         <Employee>
              <Name>Roy</Name>
              <Code>R111</Code>
              <Date type="DOJ">06/18/2018 14:36</Date>
         </Employee>
    </Employees>
</Department>'

SELECT  n.value('DeptID[1]','varchar(10)') AS DeptID,
        n.value('DepRefNum[1]','varchar(10)') AS DepRefNum,
        n.value('Date[1]/@type','varchar(10)') AS Create_Date_Type,
        n.value('Date[1]','datetime') AS Create_Date
FROM @x.nodes('/Department') R(n)

SELECT  n.value('../../DeptID[1]','varchar(10)') AS DeptID,
        n.value('Name[1]','varchar(10)') AS Emp_Name,
        n.value('Code[1]','varchar(10)') AS Emp_Code,
        n.value('Date[1]/@type','varchar(10)') AS Date_Type,
        n.value('Date[1]','datetime') AS Date
FROM @x.nodes('/Department/Employees/Employee') R(n)
示例

Declare @XML xml = '
<Department>
   <DeptID>D101</DeptID>
   <DepRefNum></DepRefNum>
   <Date type="Create">01/1/2017 12:30</Date>
   <Employees>
         <Employee>
             <Name>Jason</Name>
             <Code>J111</Code>
             <Date type="DOJ">06/18/2018 14:36</Date>
         </Employee>
         <Employee>
              <Name>Roy</Name>
              <Code>R111</Code>
              <Date type="DOJ">06/18/2018 14:36</Date>
         </Employee>
    </Employees>   
</Department>
'

Select DeptID    = lvl1.n.value('DeptID[1]','varchar(150)') 
      ,DepRefNum = lvl1.n.value('DepRefNum[1]','varchar(150)') 
      ,Create_Date_Type = lvl1.n.value('Date[1]/@type','varchar(150)') 
      ,Create_Date = lvl1.n.value('Date[1]','varchar(150)') 
 From  @XML.nodes('*') lvl1(n)

Select DeptID    = lvl1.n.value('DeptID[1]','varchar(150)') 
      ,Emp_Name  = lvl2.n.value('Name[1]','varchar(150)') 
      ,Emp_Code  = lvl2.n.value('Code[1]','varchar(150)') 
      ,Date_Type = lvl2.n.value('Date[1]/@type','varchar(150)') 
      ,Date      = lvl2.n.value('Date[1]','varchar(150)') 
 From  @XML.nodes('*') lvl1(n)
 Cross Apply lvl1.n.nodes('Employees/*') lvl2(n)
返回

DeptID      DepRefNum   Create_Date_Type    Create_Date
D101                    Create              01/1/2017 12:30


在这里提取数据不需要动态SQL。您只需查询适当的节点并访问父节点,如下所示:

DECLARE @x XML='<Department>
   <DeptID>D101</DeptID>
   <DepRefNum></DepRefNum>
   <Date type="Create">01/1/2017 12:30</Date>
   <Employees>
         <Employee>
             <Name>Jason</Name>
             <Code>J111</Code>
             <Date type="DOJ">06/18/2018 14:36</Date>
         </Employee>

         <Employee>
              <Name>Roy</Name>
              <Code>R111</Code>
              <Date type="DOJ">06/18/2018 14:36</Date>
         </Employee>
    </Employees>
</Department>'

SELECT  n.value('DeptID[1]','varchar(10)') AS DeptID,
        n.value('DepRefNum[1]','varchar(10)') AS DepRefNum,
        n.value('Date[1]/@type','varchar(10)') AS Create_Date_Type,
        n.value('Date[1]','datetime') AS Create_Date
FROM @x.nodes('/Department') R(n)

SELECT  n.value('../../DeptID[1]','varchar(10)') AS DeptID,
        n.value('Name[1]','varchar(10)') AS Emp_Name,
        n.value('Code[1]','varchar(10)') AS Emp_Code,
        n.value('Date[1]/@type','varchar(10)') AS Date_Type,
        n.value('Date[1]','datetime') AS Date
FROM @x.nodes('/Department/Employees/Employee') R(n)

我只希望OP的xml不在他们帖子中的状态,否则他们会将其存储为
varchar
,并将其转换为
xml
@Larnu,希望你是对的。:)“在此期间,我将假定复制/粘贴错误。@John Cappelletti.”。。非常感谢:)