Insert XML document into SQL Server database (2)

Source: Internet
Author: User

Insert SQL Server2000 Database in XML document (1) (http://blog.csdn.net/LoveLion/archive/2006/09/18/1237263.aspx
, Read it as an external file and then process it. By referring to relevant materials, you can use the following method to perform the operation: (assume that there is a product under the C drive of the computer. XML file, whose root node/sub-node is '/productdata/products'). The code for this file is as follows:

<? XML version = "1.0" encoding = "gb2312"?>
<Productdata>
<Products productid = "p030" productname = "Barbie doll" Description = "this is a toy for girls" Category = "Toy" Price = "120" qoh = "10"/>
<Products productid = "p031" productname = "Mini race car" Description = "this is a toy for Boys" Category = "Toy" Price = "130" qoh = "12 "/>
<Products productid = "p032" productname = "babys rattle" Description = "this is a toy for babies" Category = "Toy" Price = "80" qoh = "24"/>
</Productdata>

Run the following program in the query analyzer to insert the above data into the table products:

Declare @ filename varchar (255)
Declare @ execcmd varchar (255)
Declare @ filecontents varchar (8000)
Declare @ IDOC int
Declare @ Y int
Declare @ x int
Create Table # tempxml (Pk int not null identity (1, 1), thisline varchar (255 ))
Set @ filename = 'C:/product. xml'
Set @ execcmd = 'type' + @ filename
Set @ filecontents =''
Insert into # tempxml exec master. DBO. xp_mongoshell @ execcmd
Select @ Y = count (*) from # tempxml
Set @ x = 0
While @ x <> @ Y
Begin
Set @ x = @ x + 1
Select @ filecontents = @ filecontents + thisline from # tempxml where PK = @ x
End
Select @ filecontents as filecontents
Drop table # tempxml

Exec sp_xml_preparedocument @ IDOC output, @ filecontents
Select *
From openxml (@ IDOC, '/productdata/products', 1)
With (productid char (4 ),
Productname varchar (50 ),
Description varchar (100 ),
CATEGORY varchar (10)
)
Insert Products
Select *
From openxml (@ IDOC, '/productdata/products ')
With Products
Exec sp_xml_removedocument @ IDOC

Select * from products

This program first reads the XML document into a string variable @ filecontents cyclically in the unit of action, and creates a temporary table # tempxml to store the string generated by the XML document, after the file is processed, delete the temporary table and add @ filecontents to the method described in the http://blog.csdn.net/LoveLion/archive/2006/09/18/1237263.aspx for processing. to insert external XML documents.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.