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)
 Xquery Efficent FLOWR

Author  Topic 

bonekrusher
Starting Member

44 Posts

Posted - 2008-11-03 : 12:38:27
Hi, Take the below example:

DECLARE @x xml 
SET @x='<foo:root xmlns:foo="http://www.foo/schema">
<foo:data1>
<foo:part_number>1ASD4</foo:part_number>
<foo:smr>smrcode</foo:smr>
</foo:data1>
<foo:data2>
<foo:cage>09998</foo:cage>
<foo:part_number>12234</foo:part_number>
</foo:data2>
<foo:data2>
<foo:cage>04568</foo:cage>
<foo:part_number>1ASD4</foo:part_number>
</foo:data2>
<foo:data2>
<foo:cage>ASBB7</foo:cage>
<foo:part_number>ZZZSSD4</foo:part_number>
</foo:data2>
</foo:root>'
SELECT @x.query('declare namespace foo="http://www.foo/schema";
(<root>
{for $x in //foo:data2 return
<doc>
<pn>{$x/foo:part_number}</pn>
<cage>{$x/foo:cage}</cage>
<smr>
{for $y in //foo:data1[foo:part_number = $x/foo:part_number]
return
data($y/foo:smr)}
</smr>
</doc>}
</root>)')


returns:

<root>
<doc>
<pn>
<foo:part_number xmlns:foo="http://www.foo/schema">12234</foo:part_number>
</pn>
<cage>
<foo:cage xmlns:foo="http://www.foo/schema">09998</foo:cage>
</cage>
<smr />
</doc>
<doc>
<pn>
<foo:part_number xmlns:foo="http://www.foo/schema">1ASD4</foo:part_number>
</pn>
<cage>
<foo:cage xmlns:foo="http://www.foo/schema">04568</foo:cage>
</cage>
<smr>smrcode</smr>
</doc>
<doc>
<pn>
<foo:part_number xmlns:foo="http://www.foo/schema">ZZZSSD4</foo:part_number>
</pn>
<cage>
<foo:cage xmlns:foo="http://www.foo/schema">ASBB7</foo:cage>
</cage>
<smr />
</doc>
</root>


My inner FLOWR $y (//foo:data1[foo:part_number = $x/foo:part_number]) searches the xml for a match in the foo:data1/foo:part_number. If there is a match, it prints the following-sibling foo:smr.

Everything works, but takes a while with large documents. Is there a better way?

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-11-03 : 12:42:26
have you tried XML indexing it?

_______________________________________________
Causing trouble since 1980
Blog: http://weblogs.sqlteam.com/mladenp
Speed up SSMS development: www.ssmstoolspack.com <- version 1.1 out!
Go to Top of Page

bonekrusher
Starting Member

44 Posts

Posted - 2008-11-03 : 12:45:58
No, please explain
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-11-03 : 12:48:04
If this helps:
http://msdn.microsoft.com/en-us/library/ms345121(SQL.90).aspx
Go to Top of Page

bonekrusher
Starting Member

44 Posts

Posted - 2008-11-03 : 13:58:08
Thanks. My xml columns are indexed. I usually do this when I create the table. Is there anything else?
Go to Top of Page
   

- Advertisement -