I'm trying to load an xml document into a sql server table using vbscript and sqlxmlbulkload. The xml file has a header element with the filename, date, record counts, etc, followed by transaction elements. I'd like to merge some of the header info with the transaction data but I can't figure out how to configure the schema file to do this with a single target table insert.
Excerpt of data:
CT.xml:
<?xml version="1.0"?>
<infocdr>
<file_name>ct.xml</file_name>
<ticket>
<unique_key>29820088</unique_key>
<content_code>1234</content_code>
</ticket>
<ticket>
<unique_key>29820089</unique_key>
<content_code>5678</content_code>
</ticket>
</infocdr>
target table ddl:
CREATE TABLE [dbo].[Tickets](
[file_name] [varchar](50) NULL,
[unique_key] [varchar](10) NULL,
[content_code] [varchar](4) NULL
) ON [PRIMARY]
desired records inserted in table:
ct.xml 29820088 1234
ct.xml 29820089 5678
CT.vbs:
Set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkLoad")
objBL.ConnectionString = "provider=SQLOLEDB.1;data
source=localhost;database=northwind;integrated security=SSPI"
objBL.ErrorLogFile = "c:\temp\error1.log"
objBL.Execute "c:\CT_schema.xml", "c:\CT.xml"
Set objBL = Nothing
CT_schema.xml (so far):
<xsd:schema xmlns:xsd=" xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
<xsd:element name="file_name" type="xsd:string" sql:relation="Tickets"/>
<xsd:element name="ticket" sql:relation="Tickets">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="unique_key" type="xsd:string" />
<xsd:element name="content_code" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
but this inserts undesired results:
ct.xml NULL NULL
NULL 29820088 1234
NULL 29820089 5678
Excerpt of data:
CT.xml:
<?xml version="1.0"?>
<infocdr>
<file_name>ct.xml</file_name>
<ticket>
<unique_key>29820088</unique_key>
<content_code>1234</content_code>
</ticket>
<ticket>
<unique_key>29820089</unique_key>
<content_code>5678</content_code>
</ticket>
</infocdr>
target table ddl:
CREATE TABLE [dbo].[Tickets](
[file_name] [varchar](50) NULL,
[unique_key] [varchar](10) NULL,
[content_code] [varchar](4) NULL
) ON [PRIMARY]
desired records inserted in table:
ct.xml 29820088 1234
ct.xml 29820089 5678
CT.vbs:
Set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkLoad")
objBL.ConnectionString = "provider=SQLOLEDB.1;data
source=localhost;database=northwind;integrated security=SSPI"
objBL.ErrorLogFile = "c:\temp\error1.log"
objBL.Execute "c:\CT_schema.xml", "c:\CT.xml"
Set objBL = Nothing
CT_schema.xml (so far):
<xsd:schema xmlns:xsd=" xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
<xsd:element name="file_name" type="xsd:string" sql:relation="Tickets"/>
<xsd:element name="ticket" sql:relation="Tickets">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="unique_key" type="xsd:string" />
<xsd:element name="content_code" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
but this inserts undesired results:
ct.xml NULL NULL
NULL 29820088 1234
NULL 29820089 5678