I have a very small XML file (it's
a sample file, i'm just trying to learn how to do this right now) which
i'm inserting into a table. Once there, i'm trying to query the file
but get an empty result set. I've tried using Xquery and OpenXML but
get the same results with each method.
I really don't understand
why this doesn't return any results. All i'm trying to do is get the
book titles and the prices. I get the columns to return, but no data.
Why?
EDIT - I'm having a very difficult time trying to put XML code in here, giving me errors. I'm posting it as-is outside of the code block. Here's the XML file i'm using:
<bookstore><book><title lang="eng">Harry Potter</title><price>29.99</price></book><book><title lang="eng">Learning XML</title><price>39.95</price></book></bookstore>
Here's the code i'm using to insert the file into a table:
--CREATE TABLE TO STORE XML FILES CREATE TABLE XMLTest ( ID INT IDENTITY PRIMARY KEY ,XMLFIle XML ,LoadDate DATETIME ) GO --BULK INSERT THE XML FILE(s) INSERT INTO XMLTest (XMLFile, LoadDate) SELECT CONVERT(XML, BulkColumn) as BulkColumn, GetDate() FROM OPENROWSET(BULK 'C:\XML\Books.xml', SINGLE_BLOB) as X GO --SELECT THE FILES FROM THE TABLE TO ENSURE INSERT WORKED SELECT * FROM XMLTest
Here's my query in OpenXML:
DECLARE @XML as XML ,@HDoc as INT ,@SQL as VARCHAR(MAX) SELECT @XML = XMLFile FROM XMLTest EXEC sp_xml_preparedocument @HDoc OUTPUT, @XML SELECT * FROM OPENXML(@HDoc, 'Bookstore/Book/Title/Price') WITH ( Title [VARCHAR](50) 'Title' ,Price [DECIMAL](5,2) 'Price' ) EXEC sp_xml_removedocument @HDoc GOCan
someone please explain how I can get values the book title and price? I
have a real work project coming up in which I have to shred a larger
XML file, however I want to get the hang of doing this on a smaller file
first.
What am I doing wrong?
Thanks!!