473,398 Members | 2,427 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,398 software developers and data experts.

insert xml data into sql server?

i need to insert data from an xml file into sql server table.
the xml file contains only one record.
how to insert the data? how to map the names of the fields in the xml file
and the table?
thank you in advance.
Nov 12 '05 #1
2 52052
Assuming you're using SQL Server 2000, you could use SQLXML 3.0
(http://www.microsoft.com/downloads/d...33A9-CF10-4E22
-8004-477098A407AC&displaylang=en).

There's a couple of ways you could do this. The first is to annotate a
schema and use the SQLXMLBULKLOAD component - this is a COM-based component
that can be used to import XML into SQL Server tables based on the mappings
defined in an annotated schema. See
http://msdn.microsoft.com/library/de...kload_6bos.asp
for an example of using SQLXMLKBULKLOAD.

The other alternative is to use the OPENXML function. The idea here is to
create a SProc to which
you'll pass your XML (usually as an NTEXT parameter). In the SProc you use
sp_xml_preparedocument to generate an in-memory "DOM-like" tree structure,
and then use OPENXML to shred the XML into relational rowsets. Below is a
simple example that might help you get started (search for "Using OPENXML"
in BOL and you'll find some examples there.)

The OPENXML syntax is a little complex so here's a brief explanation:
SELECT * FROM
OPENXML(<DOM-Tree created by sp_prepare_document>,
<XPath identifying the node in the document where you
want to start the query>,
<flags: 1=search attributes by default, 2=return
subelements by default, 3 = return both attributes and subelements by
default> )
WITH (<Name of a table that matches the resultset you want | Rowset
definition consisting of column name, type, and optionally an XPath
expression if the data won't be returned by the Flags in the OPENXML clause
(the XPath is relative to the starting point specified in the OPENXML
clause)
-- Create Temporary Tables to simulate the order and order detail tables
CREATE TABLE #Orders
(OrderID INTEGER NOT NULL ,
CustomerID nCHAR (5) NULL ,
EmployeeID INTEGER NULL ,
OrderDate DATETIME NULL)

CREATE TABLE #OrderDetails
( OrderID INTEGER NOT NULL,
ProductID INTEGER NOT NULL,
UnitPrice MONEY NOT NULL,
Quantity SMALLINT NOT NULL,
Discount REAL NOT NULL )

GO
DECLARE @iTree INTEGER
DECLARE @xmlOrder VARCHAR(2000)

-- Simulate an order
SET @xmlOrder = '
<?xml version="1.0"?>
<Order OrderID="1001" CustomerID="ALFKI" EmployeeID="1"
OrderDate="01/01/2001">
<Items>
<Item ProductID="11" Qty="1" UnitPrice="12.99">
<Discount>0</Discount>
</Item>
<Item ProductID="17" Qty="2" UnitPrice="4.99">
<Discount>0.5</Discount>
</Item>
<Item ProductID="21" Qty="1" UnitPrice="11.99">
<Discount>0</Discount>
</Item>
</Items>
</Order>
'
EXEC sp_xml_preparedocument @iTree OUTPUT, @xmlOrder

--Insert Order Record
INSERT #Orders (OrderID, CustomerID, EmployeeID, OrderDate)
SELECT OrderID, CustomerID, EmployeeID, OrderDate
FROM
OPENXML(@iTree, 'Order', 1)
WITH ( OrderID INTEGER,
EmployeeID INTEGER,
OrderDate DATETIME,
CustomerID nCHAR(5))

-- Insert Order Details Record
INSERT #OrderDetails
SELECT * FROM
OPENXML(@iTree, 'Order/Items/Item', 1)
WITH ( OrderID INTEGER '../../@OrderID',
ProductID INTEGER,
Qty INTEGER,
UnitPrice MONEY,
Discount REAL 'Discount')

-- Clear the XML from memory
EXEC sp_xml_removedocument @iTree

GO

SELECT * FROM #Orders
SELECT * FROM #OrderDetails

Hope that helps.
--
Graeme Malcolm
Principal Technologist
Content Master Ltd.
www.contentmaster.com
"maltchev" <ma******@yahoo.com> wrote in message
news:4Cxvb.451547$pl3.385500@pd7tw3no...
i need to insert data from an xml file into sql server table.
the xml file contains only one record.
how to insert the data? how to map the names of the fields in the xml file
and the table?
thank you in advance.

Nov 12 '05 #2
Thank you for the detailed answer, Graeme :-)
I will try some of the things tomorrow.
Regards.
Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Santo Santis | last post by:
How can I make an .exe file that can insert data automatically in a SQLServer database, I can do it in C, but how can I connect to SQLServer and execute a query. All data that I have to insert...
1
by: Pratik Gupte | last post by:
I have created a database in .mdf format, but I am unable to insert data into its tables. Can anybody help how to insert data using ASP.Net 2.0 using SQL Server 2005 Express edition in windows...
5
seshu
by: seshu | last post by:
hi everybody this is seshu i am creating an application for chekin/checkouttime for that i need to insert data in to mysql for that i know the command in mysql and even how to do...
1
by: reagen | last post by:
dear all, please help me that my script cannot insert data to ms acces. <%@ Page Language="vb" ContentType="text/html"%> <%@ Import NameSpace = "System.Data" %> <%@ Import NameSpace =...
1
by: Doc11 | last post by:
I'm trying to allow users insert data into a database using the form view. But when I click the insert button I get this error: Server Error in '/Customer Database' Application....
7
by: girl | last post by:
Hi i have a problem with MS SQL server 2000 and hope to seek for some advise. i have the following samples aa|0|abcdefg| b|0|abcdefg| i used the bulk insert in the query analyser.. BULK...
0
by: troydixon | last post by:
Hello, I am new at this, and have been trying to insert data into a table by using the footer of a gridview (which I dont like) or by using a detials view on the same page that is doing the...
0
by: akshalika | last post by:
I am new to Biztalk. In my project we need to connect oracle database and insert data into oracle table using BizTalk project. I use WCF Adapter pack(SP2). I create biztalk project then using Consume...
3
by: southpawjoe | last post by:
Hi, I would like to insert data into two related tables in a SQL Server 2008 database. I created the following stored procedure but it has a problem. When I add a record, the State table gets...
1
by: Gurpreet Singhh | last post by:
whats error in java statement to insert data into sql server 2005??? # String query = "insert into dbo.guru VALUES('"taskdata.getTaskid() +',' + taskdata.getTaskname()...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.