Solved

nest XPath query in SQL Query

Posted on 2004-09-20
9
149 Views
Last Modified: 2012-06-27
is it possible to store an xml file in a db field
then when you want to query that xml file

you send in an XPath statement within a SQL Query to get at the stuff in the file.


-------------------


thanks
0
Comment
Question by:MrKevorkian
  • 4
  • 3
  • 2
9 Comments
 
LVL 2

Expert Comment

by:vidnan123
ID: 12100008
This link should be helpful to you.

http://www.xml.com/pub/a/2001/06/20/databases.html?page=3
0
 
LVL 2

Expert Comment

by:vidnan123
ID: 12100047
Also, have a look at this link. The features provided by the next version of SQL server (YUKON) for XML would definitely support your requirement.

http://www.developer.com/db/article.php/3294151
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 12108740
>>is it possible to store an xml file in a db field <<
Yes.

>>then when you want to query that xml file
you send in an XPath statement within a SQL Query to get at the stuff in the file.<<
You can do this with T-SQL alone, providing the Xml document is less than 8000 bytes.


0
 
LVL 1

Author Comment

by:MrKevorkian
ID: 12110762
acperkins  thanks for your comments

say i had a simple xml file like so:

----------------------------------
<Books>
<Book>
<Title>American Tabloid</Title>
<Author>James Ellroy</Author>
</Book>
<Book>
<Title>Lady of the lake</Title>
<Author>Raymond Chandler</Author>
</Book>
</Books>
----------------------------------

and i entered it as a string into a SQL field.

How then could i query it with TSQL  (my xml will always be smaller than 8k bytes)

for instance if i wanted to get a list of titles stored

thanks


0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 1

Author Comment

by:MrKevorkian
ID: 12110826
ps.  is storing the entire file in a text field the best way to do it?
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 300 total points
ID: 12112621
>>How then could i query it with TSQL<<
Difficult to know exactly without more details.  However, assuming that the table has a primary key of type integer (ID) than the something like (untested):

Create Procedure usp_Get_Books
                     @ID integer

As

Declare @XmlColumn varchar(8000),
      @iDoc integer

SET NOCOUNT ON

Select @XmlColumn = XmlColumn from Table1 Where ID = @ID

Exec sp_xml_preparedocument @iDoc OUTPUT, @XmlDocument

Select      RequestName,
      @UserName = UserName
From      OpenXML(@iDoc, 'Books/Book', 2) With (
            Title varchar(50) 'Title',
            Author varchar(50) 'Author')

EXEC sp_xml_removedocument @iDoc
0
 
LVL 1

Author Comment

by:MrKevorkian
ID: 12123191
hi acperkins

thanks 4 the code.
got an error

on the line
--**************************
Select     RequestName,
     @UserName = UserName
From     OpenXML(@iDoc, 'Books/Book', 2) With (
          Title varchar(50) 'Title',
          Author varchar(50) 'Author')

--**************************
it was:

"A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations."

any ideas?

thanks

0
 
LVL 1

Author Comment

by:MrKevorkian
ID: 12123208
no need 2 worry about that i have just got it 2 work.

back shortly
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 12124600
Sorry about that I copied and pasted some code I had and forgot to edit it, it should have been:
Declare @XmlColumn varchar(8000),
     @iDoc integer

SET NOCOUNT ON

Select @XmlColumn = XmlColumn from Table1 Where ID = @ID

Exec sp_xml_preparedocument @iDoc OUTPUT, @XmlDocument

Select     Title,
              Author
From     OpenXML(@iDoc, 'Books/Book', 2) With (
          Title varchar(50) 'Title',
          Author varchar(50) 'Author')

EXEC sp_xml_removedocument @iDoc
0

Featured Post

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

896 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now