Re: Simpler XQuery Syntax?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance




Martin:

Yes, it all works. It just seems redundant to have to restate the identical
'for' statement, once for each namespace/schema. I thought there might be a
more concise way to phrase this SELECT statement that would accomplish the
same thing.

Regards,
Bill

"Martin Honnen" wrote:

Bill D. wrote:

Look at the query below and notice that the ‘for’ statements are identical;
the only difference is the namespace declaration in the second query. This
works as intended, but it seems like a more concise representation of this
whole statement would be possible. However, I couldn’t get the syntax gods
to cooperate. ;-)

Any simplification ideas?

SELECT ComputerName
from dbo.Computers
where
cast(CurrentConfig.query('

for $x in /CurrentConfiguration/Install/V
where string($x/@n) = "TIER_SERVER"
return data($x/@d)
') as varchar(1)) = '1'

Does

WHERE
CurrentConfig.value('
(/CurrentConfiguration/Install/V[@n = "TIER_SERVER"]/@d)[1]', 'int') = 1

and



Or

cast(CurrentConfig.query('
declare default element namespace "urn:ConfigSchema_V2";

for $x in /CurrentConfiguration/Install/V
where string($x/@n) = "TIER_SERVER"
return data($x/@d)
') as varchar(1)) = '1'

OR
CurrentConfig.value('
declare default element namespace "urn:ConfigSchema_V2";
(/CurrentConfiguration/Install/V[@n = "TIER_SERVER"]/@d)[1]', 'int') = 1

do what you want?



--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/

.