Re: XQuery - Only return if not null
- From: Mike Müller <spam_mike_mueller@xxxxxxxxxxx>
- Date: Tue, 3 Feb 2009 22:30:00 +0100
Hi Martin!
Thank you so much! Thats what I need!
Thank you , thank you!
"Martin Honnen" <mahotrash@xxxxxxxx> schrieb im Newsbeitrag news:uG9d44hhJHA.1248@xxxxxxxxxxxxxxxxxxxxxxx
Mike Müller wrote:
The problem is that my XML document does sometimes contatin the ElementABC and sometimes the ElementDEF. So if the ElelmetABC is found, then i want to use it's P1 value for the ColumnA. If it is not there the i want to try if the ElementDEF is found and use it's value for ColumnA.
Here is an example:
DECLARE @x XML;
SET @x = N'<Root>
<ElementABC P1="07" P2="07"/>
<ElementDEF P1="09" P2="07" P4="43" />
<ElementGHI P1="01" P2="07" P8="43" />
</Root>';
SELECT @x.value('(/Root/ElementABC[1]/@P1, /Root/ElementDEF[1]/@P1)[1]', 'nvarchar(5)');
SET @x = N'<Root>
<ElementDEF P1="09" P2="07" P4="43" />
<ElementGHI P1="01" P2="07" P8="43" />
</Root>';
SELECT @x.value('(/Root/ElementABC[1]/@P1, /Root/ElementDEF[1]/@P1)[1]', 'nvarchar(5)');
So you simply use a sequence
(/Root/ElementABC[1]/@P1, /Root/ElementDEF[1]/@P1)
and then access its first item with
(/Root/ElementABC[1]/@P1, /Root/ElementDEF[1]/@P1)[1]
that way if ElementABC/@P1 exists, it is the first item in the sequence, if not, then ElementDEF/@P1 is the first item.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
.
- References:
- XQuery - Only return if not null
- From: Mike Müller
- Re: XQuery - Only return if not null
- From: Martin Honnen
- XQuery - Only return if not null
- Prev by Date: Re: XQuery - Only return if not null
- Next by Date: Re: Exporting SQL Server data to XML
- Previous by thread: Re: XQuery - Only return if not null
- Index(es):
Relevant Pages
|
Loading