Solved

MS SQL 2005 and @XML.exist

Posted on 2010-09-17
3
753 Views
Last Modified: 2012-05-10
I'm trying to store a list of data in XML variable and after I need to make a test to see if a string exists.


In my example the string I'm looking for is "Test4", but not exists in the XML.

Why when I used the @xml.exist('sql:variable("@xPath")')  the results is wrong?

declare @xml xml
set @xml =
'<?xml version="1.0" encoding="utf-8"?>
<attributes>
	<attribute  txt="test1" notes="blablaablalb"/>
	<attribute  txt="test2" notes="blablaablalb"/>
	<attribute  txt="test3" notes="blablaablalb"/>
</attributes>'

select 
	@xml.exist('/attributes/attribute[@txt = "test4"]') 

-- return 0

declare @xPath varchar(10)

set @xPath = '/attributes/attribute[@txt = "test4"]'

select 
	@xml.exist('sql:variable("@xPath")') 

-- return 1

Open in new window


Can you please help to understand what I'm doing wrong or how can I used @xml.exist whit dynamics SQL?


 
0
Comment
Question by:Cboudroz
[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
  • 2
3 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 33705514
Cboudroz,

This doesn't fix the ultimate issue, but your varchar(10) isn't big enough for the XPath, so fix that to varchar(50) or something of that size.  Then you may be able to use dynamic SQL like this.
declare @xPath nvarchar(50), @sql nvarchar(200)

set @xPath = N'/attributes/attribute[@txt = "test4"]'
set @sql = N'select @xmlFrag.exist('''+ @xPath +''')'

exec sp_executeSQL @sql, N'@xmlFrag xml', @xmlFrag = @xml; 

Open in new window

0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 33705534
And this may be what you are really after and why varchar was originally only set to 10.
declare @xml xml
set @xml =
'<?xml version="1.0" encoding="utf-8"?>
<attributes>
	<attribute  txt="test1" notes="blablaablalb"/>
	<attribute  txt="test2" notes="blablaablalb"/>
	<attribute  txt="test3" notes="blablaablalb"/>
</attributes>';
declare @txt varchar(10)
set @txt = 'test4';
select @txt
     , @xml.exist('/attributes/attribute[@txt = sql:variable("@txt")]')

Open in new window

0
 
LVL 7

Author Comment

by:Cboudroz
ID: 33716194
mwvisa1:

sorry for varchar(10) that wasn't the original code.  The problems was that I didn't correctly used the sql:variable function.  

Thanks for your help.



0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

691 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