保存XML嵌套子节点

时间:2015-10-05 09:32:12

标签: sql-server xml nested xquery-sql

我遇到了将XML节点保存在不同表中的问题。 我想将Listnodes保存在一个表(ListsSet)和另一个表(ColumnsSet)中的Columnsnodes,它引用了ListTable。但是在Columnsnode中也是DependentLookupField子节点。我想将DependentLookupField中的值保存在第三个表(DependentLookupFieldsSet)中,该表引用了ColumnTable。

我的XML:

<Lists>
<List title="test">
  <ListUrl>fsasa</ListUrl>
  <ListTitle>gsdfgsg</ListTitle>
  <ListDesc>jasdh</ListDesc>
  <Columns>
    <Column action="modify">
      <InternalName>Title</InternalName>
      <DisplayNameOrigin>Titel</DisplayNameOrigin>
      <DisplayName>Name</DisplayName>
    </Column>
    <Column action="new">
      <Required>true</Required>
      <FieldType>Choice</FieldType>
      <InternalName>anrede</InternalName>
      <DisplayName>Anrede</DisplayName>
    </Column>
    <Column action="add" type="sitecolumn">
      <Required>true</Required>
      <InternalName>Bank</InternalName>
      <DisplayName>Bank</DisplayName>
      <MultipleValues>0</MultipleValues>
      <DependentLookupFields>
        <DependentLookupField internalName="Title">My Value</DependentLookupField>
      </DependentLookupFields>
    </Column>
  </Columns>
</List>
<List>
 .
 .
 .
 etc
</List>
</Lists>

我的存储过程代码:

INSERT INTO ListsSet
SELECT 
List.value('ListUrl[1]','NVARCHAR(100)') AS ListURL, 
List.value('ListTitle[1]','NVARCHAR(100)') AS ListTitle, 
List.value('ListDesc[1]','NVARCHAR(100)') AS ListDesc,
FROM
@xml.nodes('/Lists/List')AS TEMPTABLE(List)

INSERT INTO ColumnsSet
SELECT 
l.ListsID, 
c.value('FieldType[1]','NVARCHAR(100)') AS FieldType,
c.value('InternalName[1]','NVARCHAR(100)') AS InternalName,
c.value('DisplayName[1]','NVARCHAR(100)') AS DisplayName,
c.value('Required[1]','NVARCHAR(100)') AS Required,
c.value('DisplayNameOrigin[1]','NVARCHAR(100)') AS DisplayNameOrigin,
c.value('MultipleValues[1]','NVARCHAR(100)') AS MultipleValues,
FROM
ListsSet AS l
cross apply @xml.nodes('/Lists/List/Columns/Column[../../ListUrl=sql:column("l.ListUrl")]') AS TEMPTABLE(c) 

INSERT INTO DependentLookupFieldsSet
SELECT
c.ColumnsID,
d.value('DependentLookupField[1]','NVARCHAR(100)') AS DependetLookupField
FROM
ColumnsSet AS c
cross apply @xml.nodes('/Lists/List/Columns/Column/DependentLookupFields/DependentLookupField[../../InternalName=sql:column("c.InternalName")]') AS TEMPTABLE(d)

保存Listsnodes和Columnsnode的代码工作正常。但是DependentLookupField的代码不会保存该值。在表&#34; DependentLookupFieldsSet&#34;我得到了引用ColumsTable(正在工作)的行,但是每行中保存的值总是为空。

感谢任何帮助!

0 个答案:

没有答案