Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Need help to find out multiple occurances

Author  Topic 

CSK
Constraint Violating Yak Guru

489 Posts

Posted - 2010-02-09 : 12:02:55
Hello Gurus,

Please help me to find out the answer for the following scenario.

I have a column that contains xml data. Like

<drink-recipe name="Fuzzy Navel">
<ingredients>
<ingredient quantity="1" unit="ounce">
Vodka
</ingredient>
<ingredient quantity="1" unit="ounce">
Peach schnapps
</ingredient>
<ingredient quantity="4" unit="ounce">
Orange juice
</ingredient>
</ingredients>
<preparation>
<step>
Pour ingredients into a highball glass almost filled with ice.
</step>
<step>
Stir.
</step>
</preparation>
</drink-recipe>



I want to findout the multiple ocurances and i have to show in output.
Like

<ingredient quantity="1" unit="ounce">, <ingredient quantity="1" unit="ounce">....

May any one help me to write query for this..?

Thanks
Krishna

Sachin.Nand

2937 Posts

Posted - 2010-02-09 : 12:20:05
You want to show the output this way?

<ingredient quantity="1" unit="ounce">, <ingredient quantity="1" unit="ounce">....

If not then the following query will display the o/p in table format

declare @tbl as table(value xml)
insert into @tbl
select'<drink-recipe name="Fuzzy Navel">
<ingredients>
<ingredient quantity="1" unit="ounce">
Vodka
</ingredient>
<ingredient quantity="1" unit="ounce">
Peach schnapps
</ingredient>
<ingredient quantity="4" unit="ounce">
Orange juice
</ingredient>
</ingredients>
<preparation>
<step>
Pour ingredients into a highball glass almost filled with ice.
</step>
<step>
Stir.
</step>
</preparation>
</drink-recipe>
'

select x.i.value('@quantity','varchar(100)')as qunatity,
x.i.value('@unit','varchar(100)')as unit
from @tbl
cross apply value.nodes('/drink-recipe/ingredients/ingredient')as x(i)



PBUH
Go to Top of Page

CSK
Constraint Violating Yak Guru

489 Posts

Posted - 2010-02-09 : 12:56:00
Idera
Thank you for the reply. and sorry for the late reply. With this above query seems okay for me.

Thank you once again
-- Krishna
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-02-09 : 13:03:47
quote:
Originally posted by CSK

Idera
Thank you for the reply. and sorry for the late reply. With this above query seems okay for me.

Thank you once again
-- Krishna




You are most welcome.

PBUH
Go to Top of Page
   

- Advertisement -