--- title: "sql:relationship and the Key Ordering Rule (SQLXML 4.0) | Microsoft Docs" ms.custom: "" ms.date: "03/17/2017" ms.prod: "sql-server-2016" ms.reviewer: "" ms.suite: "" ms.technology: - "dbe-xml" ms.tgt_pltfrm: "" ms.topic: "reference" helpviewer_keywords: - "sql:relationship" - "key ordering rules [SQLXML]" - "relationship annotation" ms.assetid: 914cb152-09f5-4b08-b35d-71940e4e9986 caps.latest.revision: 23 author: "douglaslMS" ms.author: "douglasl" manager: "jhubbard" --- # Annotation Interpretation - sql:relationship and Key Ordering Rule Because XML Bulk Load generates records as their nodes enter into scope and sends those records to Microsoft [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] as their nodes exit scope, the data for the record must be present within the scope of the node. Consider the following XSD schema, in which the one-to-many relationship between **\** and **\** elements (one customer can place many orders) is specified by using the **\** element: ``` xmlns:sql="urn:schemas-microsoft-com:mapping-schema"> ``` As the **\** element node enters into scope, XML Bulk Load generates a customer record. This record stays until XML Bulk Load reads **\**. In processing the **\** element node, XML Bulk Load uses **\** to obtain the value of the CustomerID foreign key column of the CustOrder table from the **\** parent element, because the **\** element does not specify the **CustomerID** attribute. This means that in defining the **\** element, you must specify the **CustomerID** attribute in the schema before you specify **\**. Otherwise, when an **\** element enters into scope, XML Bulk Load generates a record for the CustOrder table, and when the XML Bulk Load reaches the **\** end tag, it sends the record to [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] without the CustomerID foreign key column value. Save the schema that is provided in this example as SampleSchema.xml. ### To test a working sample 1. Create these tables: ``` CREATE TABLE Cust ( CustomerID int PRIMARY KEY, CompanyName varchar(20) NOT NULL, City varchar(20) DEFAULT 'Seattle') GO CREATE TABLE CustOrder ( OrderID varchar(10) PRIMARY KEY, CustomerID int FOREIGN KEY REFERENCES Cust(CustomerID)) GO ``` 2. Save the following sample data as SampleXMLData.xml: ``` Hanari Carnes NY 1111 Toms Spezialitten LA 1112 Victuailles en stock 1113 ``` 3. To execute XML Bulk Load, save and execute the following [!INCLUDE[msCoName](../../../includes/msconame-md.md)] Visual Basic Scripting Edition (VBScript) example as MySample.vbs: ``` set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.4.0") objBL.ConnectionString = "provider=SQLOLEDB;data source=localhost;database=tempdb;integrated security=SSPI" objBL.ErrorLogFile = "c:\error.log" objBL.CheckConstraints = True objBL.Transaction=True objBL.Execute "c:\SampleSchema.xml", "c:\SampleXMLData.xml" set objBL=Nothing ``` The result is that XML Bulk Load inserts a NULL value in the CustomerID foreign key column of the CustOrder table. If you revise the XML sample data so that the **\** child element appears before the **\** child element, you get the expected result: XML Bulk Load inserts the specified foreign key value into the column. This is the equivalent XDR schema: ``` ```