Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to import from XML into Sql Server table

Posted on 2014-11-07
3
168 Views
Last Modified: 2014-11-08
I have an xml file that looks like this that I need to insert into a sql server 2008 table:

<?xml version="1.0" encoding="utf-8"?>
<IntegrationData xmlns="http://GLTransaction.xsd" xmlns:Vendor="http://C.com/Vendor.xsd">
  <ApplicationID>PayrollCS</ApplicationID>
  <DateTime>2014-07-21T10:59:06.9540775</DateTime>
  <UserID>PayrollCS</UserID>
  <GLTransaction>
    <Reference>113</Reference>
    <Date>2014-07-07</Date>
    <Description>Terry Eddington</Description>
    <PeriodDate>2014-07-07</PeriodDate>
    <Type>Check</Type>
    <JournalEntryInformation>
      <JournalEntrySubType>Regular</JournalEntrySubType>
    </JournalEntryInformation>
    <BankTransactionInformation>
      <BankAccount>test</BankAccount>
    </BankTransactionInformation>
    <Journal>General</Journal>
    <GLTransactionDistribution>
      <Account>999</Account>
      <Amount>0.00</Amount>
      <Description>Terry Eddington</Description>
      <DisplayOrder>1</DisplayOrder>
      <IsSummaryAccount>false</IsSummaryAccount>
    </GLTransactionDistribution>
    <GLTransactionDistribution>
      <Account>762</Account>
      <Amount>0.00</Amount>
      <Description>Terry Eddington</Description>
      <DisplayOrder>15</DisplayOrder>
      <IsSummaryAccount>false</IsSummaryAccount>
    </GLTransactionDistribution>
    <BankAccount>test</BankAccount>
  </GLTransaction>
  <GLTransaction>
    <Reference>119</Reference>
    <Date>2014-07-10</Date>
    <Description>Terry Eddington</Description>
    <PeriodDate>2014-07-10</PeriodDate>
    <Type>Check</Type>
    <JournalEntryInformation>
      <JournalEntrySubType>Regular</JournalEntrySubType>
    </JournalEntryInformation>
    <BankTransactionInformation>
      <BankAccount>test</BankAccount>
    </BankTransactionInformation>
    <Journal>General</Journal>
    <GLTransactionDistribution>
      <Account>751</Account>
      <Amount>2692.31</Amount>
      <Description>Terry Eddington</Description>
      <DisplayOrder>1</DisplayOrder>
      <IsSummaryAccount>false</IsSummaryAccount>
    </GLTransactionDistribution>
    <GLTransactionDistribution>
      <Account>999</Account>
      <Amount>0.00</Amount>
      <Description>Terry Eddington</Description>
      <DisplayOrder>2</DisplayOrder>
      <IsSummaryAccount>false</IsSummaryAccount>
    </GLTransactionDistribution>
  </GLTransaction>
</IntegrationData>

Open in new window

--I am only concerned with extracting five fields and created a table to hold those five fields.  I am

create table payroll
(Account [varchar](50),
Amount numeric(14,2) ,
[Description] [varchar](100) ,
DisplayOrder int ,
IsSummaryAcct varchar(5) )

--The below executes but nothing goes into the table
DECLARE @xml XML

SELECT @xml = x.y
FROM OPENROWSET( BULK 'F:\payroll.xml', SINGLE_CLOB ) x(y)

insert into payroll (Account, Amount, [Description], DisplayOrder, IsSummaryAcct)
SELECT
      c.c.value('(Column[@Name="Account"]/text())[1]', 'VARCHAR(25)') Account,
      c.c.value('(Column[@Name="Amount"]/text())[1]', 'Numeric(14,2)') Amount,
      c.c.value('(Column[@Name="Description"]/text())[1]', 'VARCHAR(100)') [Description],
      c.c.value('(Column[@Name="DisplayOrder"]/text())[1]', 'INT') DisplayOrder,
      c.c.value('(Column[@Name="IsSummaryAcct"]/text())[1]', 'VARCHAR(5)') IsSummaryAcct
FROM @xml.nodes('IntegrationData/GLTransaction/GLTransactionDistribution') AS c(c)

Any help or suggestions would be appreciated.  Thank you.
0
Comment
Question by:tesupport
3 Comments
 
LVL 33

Accepted Solution

by:
ste5an earned 500 total points
ID: 40429029
Cause there are two problems:

1. Your XML has a default namespace.
2. Your XPath expression is not correct. Where did you get (Column[@Name="Account"]/text())[1] from??

DECLARE @xml XML = N'
<IntegrationData xmlns="http://GLTransaction.xsd" xmlns:Vendor="http://C.com/Vendor.xsd">
  <ApplicationID>PayrollCS</ApplicationID>
  <DateTime>2014-07-21T10:59:06.9540775</DateTime>
  <UserID>PayrollCS</UserID>
  <GLTransaction>
    <Reference>113</Reference>
    <Date>2014-07-07</Date>
    <Description>Terry Eddington</Description>
    <PeriodDate>2014-07-07</PeriodDate>
    <Type>Check</Type>
    <JournalEntryInformation>
      <JournalEntrySubType>Regular</JournalEntrySubType>
    </JournalEntryInformation>
    <BankTransactionInformation>
      <BankAccount>test</BankAccount>
    </BankTransactionInformation>
    <Journal>General</Journal>
    <GLTransactionDistribution>
      <Account>999</Account>
      <Amount>0.00</Amount>
      <Description>Terry Eddington</Description>
      <DisplayOrder>1</DisplayOrder>
      <IsSummaryAccount>false</IsSummaryAccount>
    </GLTransactionDistribution>
    <GLTransactionDistribution>
      <Account>762</Account>
      <Amount>0.00</Amount>
      <Description>Terry Eddington</Description>
      <DisplayOrder>15</DisplayOrder>
      <IsSummaryAccount>false</IsSummaryAccount>
    </GLTransactionDistribution>
    <BankAccount>test</BankAccount>
  </GLTransaction>
  <GLTransaction>
    <Reference>119</Reference>
    <Date>2014-07-10</Date>
    <Description>Terry Eddington</Description>
    <PeriodDate>2014-07-10</PeriodDate>
    <Type>Check</Type>
    <JournalEntryInformation>
      <JournalEntrySubType>Regular</JournalEntrySubType>
    </JournalEntryInformation>
    <BankTransactionInformation>
      <BankAccount>test</BankAccount>
    </BankTransactionInformation>
    <Journal>General</Journal>
    <GLTransactionDistribution>
      <Account>751</Account>
      <Amount>2692.31</Amount>
      <Description>Terry Eddington</Description>
      <DisplayOrder>1</DisplayOrder>
      <IsSummaryAccount>false</IsSummaryAccount>
    </GLTransactionDistribution>
    <GLTransactionDistribution>
      <Account>999</Account>
      <Amount>0.00</Amount>
      <Description>Terry Eddington</Description>
      <DisplayOrder>2</DisplayOrder>
      <IsSummaryAccount>false</IsSummaryAccount>
    </GLTransactionDistribution>
  </GLTransaction>
</IntegrationData>';

WITH XMLNAMESPACES ( DEFAULT 'http://GLTransaction.xsd' )
	SELECT	c.value('Account[1]', 'VARCHAR(25)') Account,
		c.value('Amount[1]', 'NUMERIC(14,2)') Amount,
		c.value('Description[1]', 'VARCHAR(100)') [Description],
		c.value('DisplayOrder[1]', 'INT') DisplayOrder,
		c.value('IsSummaryAccount[1]', 'VARCHAR(5)') IsSummaryAcct
	FROM	@xml.nodes('/IntegrationData/GLTransaction/GLTransactionDistribution') AS c(c);

Open in new window

0
 

Author Comment

by:tesupport
ID: 40429295
I got that from an example, which after playing with it I took that part out.  Thank you so much!
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question