Solved

nest XPath query in SQL Query

Posted on 2004-09-20
9
155 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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 datatypes, explain the available data types and show how data can be passed into and out of variables.

830 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