Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

nest XPath query in SQL Query

Posted on 2004-09-20
9
Medium Priority
?
162 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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
 
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 1200 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] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

610 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