Solved

nest XPath query in SQL Query

Posted on 2004-09-20
9
152 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

786 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