Solved

MS SQL 2005 and @XML.exist

Posted on 2010-09-17
3
747 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

785 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