Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Accessing node values in the where clause

23 views
Skip to first unread message

Pavan

unread,
Nov 27, 2009, 4:11:01 AM11/27/09
to
Hi,
I have an XML column in a table and i need to fetch the record based on some
values, that are part of the XML as nodes.
The xml format looks like below.
<a>
<b id="">
<c>valuec</c>
</b>
</a>

I need to fetch the record based on the "<c>" values.
Can anyone tell me a way, that i can use the "<c>" value in the where clause?

When i tried using "CROSS APPLY" and ".nodes" , i was getting an error, node
not defined.

Iam doing all this in sql2008

Regards,
Pavan


Martin Honnen

unread,
Nov 27, 2009, 6:14:28 AM11/27/09
to

Which where clause exactly are you talking about, SQL, XQuery?
And you might not even need any of that as XQuery simply allows e.g.
/a/b[c = "valuec"]
where you put the condition into square brackets. The posted expression
selects the 'b' child elements 'of the 'a' root element where a 'c'
child of the 'b' has the string value "valuec".

--

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

Pavan

unread,
Nov 27, 2009, 7:13:01 AM11/27/09
to
Hi Martin,
Actually i have multiple <c> nodes.
The code given by you works if i have only one <c> under <b>.

your help is appreciated.

"Martin Honnen" wrote:

> .
>

Martin Honnen

unread,
Nov 27, 2009, 7:32:47 AM11/27/09
to
Pavan wrote:

> Actually i have multiple <c> nodes.
> The code given by you works if i have only one <c> under <b>.

Please post a sample of the XML you have and explain which data you want
and which condition you want to test in plain English, then we can try
to express that with XQuery.
It is currently not clear to me what you want to achieve.

Pavan

unread,
Nov 30, 2009, 6:23:03 AM11/30/09
to
<a>
<b id="">
<c>valuec</c>
<c>valuec1</c>
<c>valuec2</c>
</b>
</a>

I would like to have a query on "valuec1"..

something like
select ...
where c = 'valuec1'

Thanks,
Pavan

"Martin Honnen" wrote:

> .
>

Martin Honnen

unread,
Nov 30, 2009, 6:46:40 AM11/30/09
to
Pavan wrote:
> <a>
> <b id="">
> <c>valuec</c>
> <c>valuec1</c>
> <c>valuec2</c>
> </b>
> </a>
>
> I would like to have a query on "valuec1"..
>
> something like
> select ...
> where c = 'valuec1'

Well the path I suggested earlier e.g.

/a/b[c = "valuec1"]

does work, it does not matter how many 'c' child elements the 'b'
element has, the condition in the square brackets is true if there is at
least one 'c' child with string value 'valuec1'.

Pavan

unread,
Nov 30, 2009, 7:32:02 AM11/30/09
to
Thanks Martin....It worked...
The XML was not proper before..


"Martin Honnen" wrote:

> .
>

0 new messages