![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi All, I have an input XML which needs to make an inner join with table on a specific field. I could not find the correct sysntax to do that. Here is my sample; DECLARE @XmlData xml SET @XmlData='<Library Subject name="ASP.NET" Book ID="1"><Author>Lakhan Pal Garg</Author><Title>ASP.NET Tips</Title><Price>$100</Price></Book Book ID="2"><Author>Lakhan Pal Garg</Author><Title>SQL Server Tips</Title><Price>$90</Price></Book /Subject Subject name="XML" Book ID="3"><Author>Peter</Author><Title>XSLT Tutorial</Title><Price>$140</Price></Book Book ID="4"><Author>Rihana</Author><Title>XML Parsing in SQL Server</Title><Price>$120</Price></Book /Subject /Library>' select R.i.value('@ID', 'varchar(30)') [BookID], R.i.query('Author').value('.', 'varchar(30)') [Author], R.i.query('Title').value('.', 'varchar(30)') [Title], R.i.query('Price').value('.', 'varchar(30)') [Price] from @XmlData.nodes('/Library/Subject/Book') R(i) If in the example above, I have Author names in the database, how would i load only those that matched my Authors table? |
#3
| |||
| |||
|
|
I have an input XML which needs to make an inner join with table on a specific field. |
#4
| |||
| |||
|
#5
| |||
| |||
|
|
What if the join condition was outside of <Subject> but inside Library? |
#6
| |||
| |||
|
#7
| |||
| |||
|
|
Ben wrote: Hi All, I have an input XML which needs to make an inner join with table on a specific field. I could not find the correct sysntax to do that. Here is my sample; DECLARE @XmlData xml SET @XmlData='<Library Subject name="ASP.NET" Book ID="1"><Author>Lakhan Pal Garg</Author><Title>ASP.NET Tips</Title><Price>$100</Price></Book Book ID="2"><Author>Lakhan Pal Garg</Author><Title>SQL Server Tips</Title><Price>$90</Price></Book /Subject Subject name="XML" Book ID="3"><Author>Peter</Author><Title>XSLT Tutorial</Title><Price>$140</Price></Book Book ID="4"><Author>Rihana</Author><Title>XML Parsing in SQL Server</Title><Price>$120</Price></Book /Subject /Library>' select R.i.value('@ID', 'varchar(30)') [BookID], R.i.query('Author').value('.', 'varchar(30)') [Author], R.i.query('Title').value('.', 'varchar(30)') [Title], R.i.query('Price').value('.', 'varchar(30)') [Price] from @XmlData.nodes('/Library/Subject/Book') R(i) If in the example above, I have Author names in the database, how would i load only those that matched my Authors table? Assuming that table is named 'Authors' and has a column named 'Name' one way is a follows: select R.i.value('@ID', 'varchar(30)') [BookID], R.i.query('Author').value('.', 'varchar(30)') [Author], R.i.query('Title').value('.', 'varchar(30)') [Title], R.i.query('Price').value('.', 'varchar(30)') [Price] from @XmlData.nodes('/Library/Subject/Book') R(i) WHERE EXISTS( SELECT NULL FROM Authors A WHERE A.Name = R.i.value('(Author)[1]', 'nvarchar(20)') ); -- Martin Honnen --- MVP XML http://msmvps.com/blogs/martin_honnen/ |
![]() |
| Thread Tools | |
| Display Modes | |
| |