473,394 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to insert Identity value to another table in SQLXML

Maidenz08
I am using SQLXML 4.0 to Bulkload data from the XML to the SQL Database. I have 2 tables. "Country" and "Customer". Below is my XML file format ("Data.xml")

Expand|Select|Wrap|Line Numbers
  1. <Root>
  2.    <Countries>
  3.        <Country Name="USA"></Country>
  4.        <Country Name="Australia"></Country>
  5.    </Countries>
  6.    <Customers>
  7.        <Customer Name="John Smith" CountryName="Australia"></Customer>
  8.    </Customers>
  9. </Root>
  10.  
I have 2 tables

Country

Id Name

1 USA
2 Australia

Customer

Id CustomerName CountryId

1 John Smith 2

When i import my XML, "Id" column in the country is auto generated as it is identity(1,1). I want this "Id" value to sit in my Customer Table "CountryId" Column.

Note that I cannot nest the Customer Tag within the Country Tag. By nesting, i can easily define Parent and Child key in the XSD. But without nesting the elements i have not found a way to define these relationships.

Below is my XSD (Schema.xml)

Expand|Select|Wrap|Line Numbers
  1. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  2.             xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
  3.   <xsd:annotation>
  4.     <xsd:appinfo>
  5.       <sql:relationship name="Country_Customer"
  6.             parent="Country"
  7.             parent-key="Id"
  8.             child="Customer"
  9.             child-key="CountryId" />
  10.     </xsd:appinfo>
  11.   </xsd:annotation>
  12.   <xsd:element name="Root" sql:is-constant="1">
  13.     <xsd:complexType>
  14.       <xsd:sequence>
  15.         <xsd:element name="Countries" sql:is-constant="1">
  16.           <xsd:complexType>
  17.             <xsd:sequence>
  18.               <xsd:element maxOccurs="unbounded" name="Country" sql:relation="Country">
  19.                 <xsd:complexType>
  20.                   <xsd:attribute name="Name" type="xsd:string" use="required" />
  21.                 </xsd:complexType>
  22.               </xsd:element>
  23.             </xsd:sequence>
  24.           </xsd:complexType>
  25.         </xsd:element>
  26.         <xsd:element name="Customers" sql:is-constant="1">
  27.           <xsd:complexType>
  28.             <xsd:sequence>
  29.               <xsd:element name="Customer" sql:relation="Customer" sql:relationship="Country_Customer">
  30.                 <xsd:complexType>
  31.                   <xsd:attribute name="Name" type="xsd:string" use="required" sql:field="CustomerName" />
  32.                   <xsd:attribute name="CountryName" type="xsd:string" use="required" sql:field="CountryId" />
  33.                 </xsd:complexType>
  34.               </xsd:element>
  35.             </xsd:sequence>
  36.           </xsd:complexType>
  37.         </xsd:element>
  38.       </xsd:sequence>
  39.     </xsd:complexType>
  40.   </xsd:element>
  41. </xsd:schema>
  42.  
I have defined an SQL relationship but i am specifying the Country Name in my XML and hence the relationship does not work.

Below are the table creation scripts


Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE [dbo].[Country](
  2.     [Id] [bigint] IDENTITY(1,1) NOT NULL PRIMARY KEY,
  3.     [Name] [nvarchar](50) NULL,
  4.  )
  5.  
  6. CREATE TABLE [dbo].[Customer](
  7.     [Id] [bigint] IDENTITY(1,1) NOT NULL Primary Key,
  8.     [CustomerName] [nvarchar](50) NULL,
  9.     [CountryId] [bigint] NULL,
  10.  )
  11.  

I am using the below VB Script to bulk import


Expand|Select|Wrap|Line Numbers
  1. Dim FileValid
  2.  
  3. set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.4.0")
  4. objBL.ConnectionString = "provider=SQLOLEDB;data source=ServerName;database=databasename;User Id=username;Password=password"
  5. objBL.ErrorLogFile = "c:\error.log"
  6. objBL.KeepIdentity = False
  7.  
  8. 'Validate the data file prior to bulkload
  9. Dim sOutput 
  10. sOutput = ValidateFile("Data.xml", "", "Schema.xml")
  11. WScript.Echo sOutput
  12.  
  13. If FileValid Then
  14.    ' Check constraints and initiate transaction (if needed)
  15.    ' objBL.CheckConstraints = True
  16.    ' objBL.Transaction=True
  17.   'Execute XML bulkload using file.
  18.   objBL.Execute "Schema.xml", "Data.xml"
  19.   set objBL=Nothing
  20. End If
  21.  
  22. Function ValidateFile(strXmlFile,strUrn,strXsdFile)
  23.  
  24.    ' Create a schema cache and add SampleSchema.xml to it.
  25.    Dim xs, fso, sAppPath
  26.    Set fso = CreateObject("Scripting.FileSystemObject") 
  27.    Set xs = CreateObject("MSXML2.XMLSchemaCache.6.0")
  28.    sAppPath = fso.GetFolder(".") 
  29.    xs.Add strUrn, sAppPath & "\" & strXsdFile
  30.  
  31.    ' Create an XML DOMDocument object.
  32.    Dim xd 
  33.    Set xd = CreateObject("MSXML2.DOMDocument.6.0")
  34.  
  35.    ' Assign the schema cache to the DOM document.
  36.    ' schemas collection.
  37.  
Mar 4 '14 #1
0 1647

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: brent.ryan | last post by:
How do I get the next int value for a column before I do an insert in MY SQL Server 2000? I'm currently using Oracle sequence and doing something like: select seq.nextval from dual; Then I...
10
by: jaYPee | last post by:
as of now i am using a stored procedure from my sql server 2000 to insert record from another table. what i need now is on how can i insert record by not using the stored procedure and insert it...
7
by: grummanf6f | last post by:
Hello Gurus, this probably is real simple for you but for me it's a bummer. I have one table in which I have basic data collected of schools. I have another data that is detailed info on the...
15
by: gunnar.sigurjonsson | last post by:
I´m having some problem retrieving identity value from my newly inserted row into a view. I have two tables T1 and T2 which I define as following CREATE TABLE T1 ( id BIGINT GENERATED ALWAYS...
1
by: billa856 | last post by:
Hi, I am trying to insert Null value in column(ShipDate) in my table.That column(ShipDate)'s type id date/time and format is short date. I am using "" to insert Null in that column(ShipDate)...
6
by: vikvikash | last post by:
hi i have split the data using the split command now i want to insert the split data to be inserted into another table. there r many records in the previous table i want each record to be split and...
1
by: kimsengora | last post by:
Ex. I need to fill in values argument Field1 is text Field2 is image Insert into tbl1 values("'+val1+"','+val2+'") val2 is image like...
1
by: dougancil | last post by:
I have a form that fills a table adapter with this: Me.Scratchpad3TableAdapter.Fill(Me.MDRDataSet.scratchpad3 and what I'd like to do is when users edit something on this form that they post...
2
by: latifju | last post by:
I am new in access. Now I am using MS Access 2010. Problem: I have table named customer with 6fields as follow 1. ID 2. Name 3. Received 4. Paid 5. Balance 6. Date
0
by: sreesrinivas1 | last post by:
hi all i have two servers each having a table in them, i'm able to connect to them using php. But when i try to select the values from one table of a server to insert them into another table in...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.