如何将标识值插入SQLXML中的另一个表

时间:2014-03-04 06:28:48

标签: sqlxml

我正在使用SQLXML 4.0将数据从XML批量加载到SQL数据库。我有2张桌子。 “国家”和“客户”。下面是我的XML文件格式(“Data.xml”)

<Root>
   <Countries>
       <Country Name="USA"></Country>
       <Country Name="Australia"></Country>
   </Countries>
   <Customers>
       <Customer Name="John Smith" CountryName="Australia"></Customer>
   </Customers>
</Root>

我有2张桌子

国家

Id名称

1美国 2澳大利亚

客户

Id CustomerName CountryId

1 John Smith 2

当我导入我的XML时,国家/地区中的“Id”列是自动生成的,因为它是标识(1,1)。我想将此“Id”值放在我的客户表“CountryId”列中。

请注意,我无法将客户标记嵌套在国家/地区标记中。通过嵌套,我可以在XSD中轻松定义父键和子键。但是,如果没有嵌套元素,我还没有找到定义这些关系的方法。

下面是我的XSD(Schema.xml)

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
  <xsd:annotation>
    <xsd:appinfo>
      <sql:relationship name="Country_Customer"
            parent="Country"
            parent-key="Id"
            child="Customer"
            child-key="CountryId" />
    </xsd:appinfo>
  </xsd:annotation>
  <xsd:element name="Root" sql:is-constant="1">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Countries" sql:is-constant="1">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element maxOccurs="unbounded" name="Country" sql:relation="Country">
                <xsd:complexType>
                  <xsd:attribute name="Name" type="xsd:string" use="required" />
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
        <xsd:element name="Customers" sql:is-constant="1">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="Customer" sql:relation="Customer" sql:relationship="Country_Customer">
                <xsd:complexType>
                  <xsd:attribute name="Name" type="xsd:string" use="required" sql:field="CustomerName" />
                  <xsd:attribute name="CountryName" type="xsd:string" use="required" sql:field="CountryId" />
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

我已经定义了一个SQL关系,但我在XML中指定了Country Name,因此这种关系不起作用。

以下是表格创建脚本

CREATE TABLE [dbo].[Country](
    [Id] [bigint] IDENTITY(1,1) NOT NULL PRIMARY KEY,
    [Name] [nvarchar](50) NULL,
 )

CREATE TABLE [dbo].[Customer](
    [Id] [bigint] IDENTITY(1,1) NOT NULL Primary Key,
    [CustomerName] [nvarchar](50) NULL,
    [CountryId] [bigint] NULL,
 )

我使用下面的VB脚本批量导入

Dim FileValid

set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.4.0")
objBL.ConnectionString = "provider=SQLOLEDB;data source=ServerName;database=databasename;User Id=username;Password=password"
objBL.ErrorLogFile = "c:\error.log"
objBL.KeepIdentity = False

'Validate the data file prior to bulkload
Dim sOutput 
sOutput = ValidateFile("Data.xml", "", "Schema.xml")
WScript.Echo sOutput

If FileValid Then
   ' Check constraints and initiate transaction (if needed)
   ' objBL.CheckConstraints = True
   ' objBL.Transaction=True
  'Execute XML bulkload using file.
  objBL.Execute "Schema.xml", "Data.xml"
  set objBL=Nothing
End If

Function ValidateFile(strXmlFile,strUrn,strXsdFile)

   ' Create a schema cache and add SampleSchema.xml to it.
   Dim xs, fso, sAppPath
   Set fso = CreateObject("Scripting.FileSystemObject") 
   Set xs = CreateObject("MSXML2.XMLSchemaCache.6.0")
   sAppPath = fso.GetFolder(".") 
   xs.Add strUrn, sAppPath & "\" & strXsdFile

   ' Create an XML DOMDocument object.
   Dim xd 
   Set xd = CreateObject("MSXML2.DOMDocument.6.0")

   ' Assign the schema cache to the DOM document.
   ' schemas collection.
   Set xd.schemas = xs

   ' Load XML document as DOM document.
   xd.async = False
   xd.Load sAppPath & "\" & strXmlFile

   ' Return validation results in message to the user.
   If xd.parseError.errorCode <> 0 Then
        ValidateFile = "Validation failed on " & _
             strXmlFile & vbCrLf & _
             "=====================" & vbCrLf & _
             "Reason: " & xd.parseError.reason & _
             vbCrLf & "Source: " & _
             xd.parseError.srcText & _
             vbCrLf & "Line: " & _
             xd.parseError.Line & vbCrLf
             FileValid = False
    Else
        ValidateFile = "Validation succeeded for " & _
             strXmlFile & vbCrLf & _
             "======================" & _
             vbCrLf & "Contents to be bulkloaded" & vbCrLf
             FileValid = True
    End If
End Function

1 个答案:

答案 0 :(得分:0)

希望这可能有所帮助: SQL Server XML