Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

how to import an xml file to sql

 
Goto page Previous  1, 2
   Database Forums (Home) -> Data Warehouse RSS
Next:  Cluster Issue  
Author Message
Dee7

External


Since: Sep 09, 2004
Posts: 41



(Msg. 16) Posted: Thu Jan 17, 2008 9:06 am
Post subject: RE: how to import an xml file to sql [Login to view extended thread Info.]
Archived from groups: microsoft>public>sqlserver>datawarehouse (more info?)

I ran the following:


declare @xml varchar(max)
declare @xmlHandle int

-- Bulk load the xml from the file
set @xml = (
select *
from openrowset
(
bulk 'C:\database\xmldtd\yahoostore.xml'
,single_blob
) Products
)

-- Prepare the DOM document
exec sp_xml_preparedocument
@xmlHandle output
,@xml

-- Parse the XML
-- You can include this select statement in your insert statement
select *
from openxml
(
@xmlHandle
,'/Products/Product'
)
with (
id varchar(32) '@Id'
,[description] varchar(1024) 'Description'
,url varchar(1024) 'Url'
,caption varchar(1024) 'Caption'
,captionnohtml varchar (1024) 'CaptionNoHTML'
,code varchar(32) 'Code'
,baseprice varchar (225) 'BasePrice'
,salesprice varchar (225) 'SalesPrice'
,categories varchar (225) 'Categories'
,thumb varchar (1024) 'Thumb'
,thumbheight varchar (255) 'ThumbHeight'
,thumbwidth varchar (255) 'ThumbWidth'
,picture varchar (1024) 'Picture'
,pictureheight varchar (255) 'PictureHeight'
,picturewidth varchar (255) 'PictureWidth'
,weight varchar (255) 'Weight'
,orderable varchar(3) 'Orderable'
,taxable varchar(3) 'Taxable'
,[path] varchar(1024) 'Path'
,localizedbaseprice varchar (255) 'LocalizedBasePrice'
,availability varchar (255) 'Availability'
,options varchar (3000) 'Options'
,sku varchar (255) 'SKU'
,optionid varchar (225) 'OptionId'
,optionname varchar (255) 'OptionName'
,optionparentid varchar (255) 'OptionParentID'
,productref_id varchar (255) 'ProductRef_ID'
,productref_url varchar (1024) 'ProductRef_URL'
,productref_name varchar (255) 'ProductRef_Name'
,itemsold varchar (255) 'ItemsSold'
,orders varchar (255) 'Orders'
,revenue varchar (255) 'Revenue'
,pageviews varchar (255) 'PageViews'
,item varchar (255) 'Item'

)

-- Never forget to remove the DOM document as soon as you're done using it.
exec sp_xml_removedocument
@xmlHandle
go



I received the following error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '('.


Thanks
Dee





"Dee" wrote:

> By reading in the xml with excel or flat files in pieces here is what I got
> in sql scripts:
>
> I could not import the data or not all of it doing it that way ran into
> errors. The option field is very large so I made it more then 255. The
> exactual xml file is 95,529 kb. Will it all get imported? Do I strill use
> the info you sent me?
>
> [Id] [nvarchar](255) NULL,
> [Description] [nvarchar](255) NULL,
> [Url] [nvarchar](255) NULL,
> [Caption] [nvarchar](max) NULL,
> [CaptionNoHTML] [nvarchar](max) NULL,
> [Code] [nvarchar](255) NULL,
> [BasePrice] [float] NULL,
> [SalePrice] [float] NULL,
> [Categories] [nvarchar](255) NULL,
> [Thumb] [nvarchar](255) NULL,
> [ThumbHeight] [float] NULL,
> [ThumbWidth] [float] NULL,
> [Picture] [nvarchar](255) NULL,
> [PictureHeight] [float] NULL,
> [PictureWidth] [float] NULL,
> [Weight] [float] NULL,
> [Orderable] [nvarchar](255) NULL,
> [Taxable] [nvarchar](255) NULL,
> [LocalizedBasePrice] [float] NULL,
> [Availability] [nvarchar](255) NULL,
> [options] [nvarchar](3000) NULL,
> [SKU] [nvarchar](255) NULL,
> [OptionID] [nvarchar](255) NULL,
> [OptionName] [nvarchar](255) NULL,
> [OptionParentID] [nvarchar](255) NULL
> [Id] [nvarchar](255) NULL,
> [ProductRef_ID] [nvarchar](255) NULL,
> [ProductRef_Url] [nvarchar](255) NULL,
> [ProductRef_Name] [nvarchar](255) NULL
> [Items Sold] [float] NULL,
> [Orders] [float] NULL,
> [Revenue] [float] NULL,
> [Page Views] [float] NULL,
> [Item] [nvarchar](255) NULL,
> [SKU] [nvarchar](255) NULL
>
> Hope this was the information you wanted.
>
> Thanks Dee
>
>
> "Dee" wrote:
>
> > Thank you so much. I download booksonline and will view it.
> >
> > For some reason, I could not get my SSIS open in the database, but I see
> > that the intergration manager is installed and running. Also for some
> > reason I could not get that create to work as it showed on the web site you
> > attached.
> >
> > But I like you feel you have given me enough so I can start and see if it
> > works.
> >
> > I will be testing all of this tomorrow. Reading tonight.
> >
> > Thank you so much for the help and I will get the information needed.
> >
> > Dee
> >
> > "ML" wrote:
> >
> > > As you can see in my example, I guessed the data types. I was hoping to see
> > > the table definition (CREATE TABLE statements), but I think you'll manage. Smile
> > >
> > > Adding columns is the easy part - just follow the example. Also, look up
> > > OPENXML in Books Online - there are some nice examples there as well. In
> > > fact, look up all keywords that are new to you in Books Online.
> > >
> > >
> > > ML
> > >
> > > ---
> > > Matija Lah, SQL Server MVP
> > > http://milambda.blogspot.com/

 >> Stay informed about: how to import an xml file to sql 
Back to top
Login to vote
Dee7

External


Since: Sep 09, 2004
Posts: 41



(Msg. 17) Posted: Thu Jan 17, 2008 10:43 am
Post subject: RE: how to import an xml file to sql [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

OK I got the openrowset to run, but it is not putting data into the database.
How do I do this.

Thanks
Dee

"Dee" wrote:

> By reading in the xml with excel or flat files in pieces here is what I got
> in sql scripts:
>
> I could not import the data or not all of it doing it that way ran into
> errors. The option field is very large so I made it more then 255. The
> exactual xml file is 95,529 kb. Will it all get imported? Do I strill use
> the info you sent me?
>
> [Id] [nvarchar](255) NULL,
> [Description] [nvarchar](255) NULL,
> [Url] [nvarchar](255) NULL,
> [Caption] [nvarchar](max) NULL,
> [CaptionNoHTML] [nvarchar](max) NULL,
> [Code] [nvarchar](255) NULL,
> [BasePrice] [float] NULL,
> [SalePrice] [float] NULL,
> [Categories] [nvarchar](255) NULL,
> [Thumb] [nvarchar](255) NULL,
> [ThumbHeight] [float] NULL,
> [ThumbWidth] [float] NULL,
> [Picture] [nvarchar](255) NULL,
> [PictureHeight] [float] NULL,
> [PictureWidth] [float] NULL,
> [Weight] [float] NULL,
> [Orderable] [nvarchar](255) NULL,
> [Taxable] [nvarchar](255) NULL,
> [LocalizedBasePrice] [float] NULL,
> [Availability] [nvarchar](255) NULL,
> [options] [nvarchar](3000) NULL,
> [SKU] [nvarchar](255) NULL,
> [OptionID] [nvarchar](255) NULL,
> [OptionName] [nvarchar](255) NULL,
> [OptionParentID] [nvarchar](255) NULL
> [Id] [nvarchar](255) NULL,
> [ProductRef_ID] [nvarchar](255) NULL,
> [ProductRef_Url] [nvarchar](255) NULL,
> [ProductRef_Name] [nvarchar](255) NULL
> [Items Sold] [float] NULL,
> [Orders] [float] NULL,
> [Revenue] [float] NULL,
> [Page Views] [float] NULL,
> [Item] [nvarchar](255) NULL,
> [SKU] [nvarchar](255) NULL
>
> Hope this was the information you wanted.
>
> Thanks Dee
>
>
> "Dee" wrote:
>
> > Thank you so much. I download booksonline and will view it.
> >
> > For some reason, I could not get my SSIS open in the database, but I see
> > that the intergration manager is installed and running. Also for some
> > reason I could not get that create to work as it showed on the web site you
> > attached.
> >
> > But I like you feel you have given me enough so I can start and see if it
> > works.
> >
> > I will be testing all of this tomorrow. Reading tonight.
> >
> > Thank you so much for the help and I will get the information needed.
> >
> > Dee
> >
> > "ML" wrote:
> >
> > > As you can see in my example, I guessed the data types. I was hoping to see
> > > the table definition (CREATE TABLE statements), but I think you'll manage. Smile
> > >
> > > Adding columns is the easy part - just follow the example. Also, look up
> > > OPENXML in Books Online - there are some nice examples there as well. In
> > > fact, look up all keywords that are new to you in Books Online.
> > >
> > >
> > > ML
> > >
> > > ---
> > > Matija Lah, SQL Server MVP
> > > http://milambda.blogspot.com/

 >> Stay informed about: how to import an xml file to sql 
Back to top
Login to vote
ML

External


Since: Mar 30, 2006
Posts: 121



(Msg. 18) Posted: Tue Jan 22, 2008 5:16 pm
Post subject: RE: how to import an xml file to sql [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Sorry for the delay.

You have to add an INSERT statement to the script.

E.g.:
Change this:
-- Parse the XML
-- You can include this select statement in your insert statement
select *

....to this:
-- Parse the XML
insert <table name>
(
<column list>
)
select <column list>
...


ML

---
Matija Lah, SQL Server MVP
http://milambda.blogspot.com/
 >> Stay informed about: how to import an xml file to sql 
Back to top
Login to vote
Dee7

External


Since: Sep 09, 2004
Posts: 41



(Msg. 19) Posted: Mon Jan 28, 2008 5:29 am
Post subject: RE: how to import an xml file to sql [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Mike,

I ran the following and it worked:

DECLARE @x XML

SELECT @x =cast(bulkcolumn as XML)

FROM OPENROWSET(BULK 'C:\database\xmldtd\yahoostore.xml', SINGLE_BLOB) AS x
USE yahoostore
-- create a table variable
CREATE TABLE PRODUCTS (ProductID VARCHAR(255), ProductDesc VARCHAR(255), Url
VARCHAR(255), Caption VARCHAR(255),
CaptionNoHtml varchar (255), Code varchar(255), BasePrice varchar(255),
SalesPrice varchar(255), Categories varchar(255),
Thumb varchar(255), ThumbHeight varchar (255), ThumbWidth varchar (255),
Picture varchar(255), PictureHeight varchar (255),
Weight varchar(255), Orderable varchar(255), Taxable varchar(255), HTMLPath
varchar(255), LocalizedBasePrice varchar(255),
Availability varchar(255), Options varchar(255), SKU varchar(255), OptionID
varchar(255), OptionName varchar(255),
OptionParentID varchar (255), ProductRef_ID varchar(255), ProductRef_URL
varchar(255), ProductRef_Name varchar(255),
ItemsSold varchar(255), Orders varchar(255), Revenue varchar(255), PageViews
varchar(255), Item varchar(255))



INSERT INTO Products (ProductID, ProductDesc, Url, Caption, CaptionNoHtml,
Code, BasePrice, SalesPrice, Categories,
Thumb, ThumbHeight, ThumbWidth, Picture, PictureHeight, Weight, Orderable,
Taxable, HTMLPath, LocalizedBasePrice,
Availability, Options, SKU, OptionID, OptionName, OptionParentID,
ProductRef_ID, ProductRef_URL, ProductRef_Name,
ItemsSold, Orders, Revenue, PageViews, Item)

SELECT

x.value('@Id[1]','varchar(255)') AS id,
x.value('Description[1]','VARCHAR(255)') as description,
x.value('Url[1]','VARCHAR(255)') as url,
x.value('Caption[1]','VARCHAR(255)') as caption,
x.value('CaptionNoHtml[1]', 'VARCHAR(255)') AS captionnohtml,
x.value('Code[1]', 'VARCHAR(255)') AS code,
x.value('BasePrice[1]', 'VARCHAR(255)') AS baseprice,
x.value('SalesPrice[1]', 'VARCHAR(255)') AS salesprice,
x.value('Categories[1]', 'VARCHAR(255)') AS categories,
x.value('Thumb[1]', 'VARCHAR(255)') AS thumb,
x.value('ThumbHeight[1]', 'VARCHAR(255)') AS thumbheight,
x.value('ThumbWidth[1]', 'VARCHAR(255)') AS thumbwidth,
x.value('Picture[1]', 'VARCHAR(255)') AS picture,
x.value('PictureHeight[1]', 'VARCHAR(255)') as pictureheight,
x.value('Weight[1]', 'VARCHAR(255)') AS weight,
x.value('Orderable[1]', 'VARCHAR(255)') AS orderable,
x.value('Taxable[1]', 'VARCHAR(255)') AS taxable,
x.value('Path[1]', 'VARCHAR(255)') AS htmlpath,
x.value('LocalizedBasePrice[1]', 'VARCHAR(255)') AS localizedbaseprice,
x.value('Availability[1]', 'VARCHAR(255)') AS availability,
x.value('Options[1]', 'VARCHAR(255)') AS options,
x.value('SKU[1]', 'VARCHAR(255)') AS sku,
x.value('OptionID[1]', 'VARCHAR(255)') AS optionid,
x.value('OptionName[1]', 'VARCHAR(255)') AS optionname,
x.value('OptionParentID[1]', 'VARCHAR(255)') AS optionparentid,
x.value('ProductRef_ID[1]', 'VARCHAR(255)') AS productrefid,
x.value('ProductRef_URL[1]', 'VARCHAR(255)') AS productrefurl,
x.value('ProductRef_Name[1]', 'VARCHAR(255)') AS productrefname,
x.value('ItemsSold[1]', 'VARCHAR(255)') AS itemssold,
x.value('Orders[1]', 'VARCHAR(255)') AS orders,
x.value('Revenue[1]', 'VARCHAR(255)') AS revenue,
x.value('PageViews[1]', 'VARCHAR(255)') AS pageviews,
x.value('Item[1]', 'VARCHAR(255)') AS item


FROM @x.nodes('/StoreExport/Products/Product') s(x)


I have another question, but it is regarding xsd. Should I start another
post for this.

Thank you
Dee

"ML" wrote:

> Sorry for the delay.
>
> You have to add an INSERT statement to the script.
>
> E.g.:
> Change this:
> -- Parse the XML
> -- You can include this select statement in your insert statement
> select *
>
> ...to this:
> -- Parse the XML
> insert <table name>
> (
> <column list>
> )
> select <column list>
> ...
>
>
> ML
>
> ---
> Matija Lah, SQL Server MVP
> http://milambda.blogspot.com/
 >> Stay informed about: how to import an xml file to sql 
Back to top
Login to vote
ML

External


Since: Mar 30, 2006
Posts: 121



(Msg. 20) Posted: Mon Jan 28, 2008 5:52 am
Post subject: RE: how to import an xml file to sql [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

If it's another issue then consider starting a new thread, note that this
particualr newsgroup is dedicated to data warehousing.

If the question has to do with T-SQL, you'll have more luck in this newsgroup:
http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public....server.

If the question has to do with XML (and XML Schema) in SQL Server:
http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public....server.

If it's a general XML Schema question you could also try here:
http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.xml


ML

---
Matija Lah, SQL Server MVP
http://milambda.blogspot.com/
 >> Stay informed about: how to import an xml file to sql 
Back to top
Login to vote
Dee7

External


Since: Sep 09, 2004
Posts: 41



(Msg. 21) Posted: Mon Jan 28, 2008 6:41 am
Post subject: RE: how to import an xml file to sql [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Mike,

I would like to thank you so much. You help and gave me ideas where to
research.

Thank you so much for you time and patience with someone struggling.

Dee

"ML" wrote:

> If it's another issue then consider starting a new thread, note that this
> particualr newsgroup is dedicated to data warehousing.
>
> If the question has to do with T-SQL, you'll have more luck in this newsgroup:
> http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public....server.
>
> If the question has to do with XML (and XML Schema) in SQL Server:
> http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public....server.
>
> If it's a general XML Schema question you could also try here:
> http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.xml
>
>
> ML
>
> ---
> Matija Lah, SQL Server MVP
> http://milambda.blogspot.com/
 >> Stay informed about: how to import an xml file to sql 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Log Import Error - I have a data warehouse that imports all of the required profile, transactions, logs, etc. on a daily basis and has been running fine. The other night, I started to receive the following errors during the web log import. My package logging shows:....

import a cube created in AS 2000 into SSAA 2005 - Is there any way to import a cube created in AS 2000 into SSAA 2005? If not a direct import is there any way to make that job easier? Any suggestions/pointer/comments would be helpful. I have a lot of AS 2000 cubes and I am really sick of that..

How to use the bcp command prompt utility to import data i.. - How to use the bcp command prompt utility to import data into the "Test" table Click Start->Run->cmd. In the command window , type cd c:\ which will take you to the root of c:\. At the C:\ prompt, type bcp Testdb..Test in Test.tx...

Row of data interchaged automatically when import from MS .. - Hi all, I import MS Excel 2003 spread sheet in MS SQL Server 2000 through MS SQL Server 2000 Enterprise Manager. In excel two sheets (sheet1 and sheet2) of data is there. I imported first sheet data in first time in relevant table of database and don...

can ommiting log file speed up me and how - Dear friends, I have a database with simple stucture and tasks but big data sizes (23M rec, 35G data file). Most of my data are static and I dn't need transaction facilities. so I set it to simple mode. The log file is created and has good size. How..
   Database Forums (Home) -> Data Warehouse All times are: Pacific Time (US & Canada)
Goto page Previous  1, 2
Page 2 of 2

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]