Solved

MS SQL 2005 and @XML.exist

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

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…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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…
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.

739 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