Solved

MS SQL 2005 and @XML.exist

Posted on 2010-09-17
3
748 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
  • 2
3 Comments
 
LVL 59

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 59

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

829 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