Solved

nest XPath query in SQL Query

Posted on 2004-09-20
9
147 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

708 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

15 Experts available now in Live!

Get 1:1 Help Now