/* Thanks for your interest in my article. If you have any questions, please feel free to contact me at ktegels@develop.com. (C) 2005 by Kent Tegels, All Rights Reserved. */ use master go select 'If the next command generates an error, that is okay, it just means you did not already have the database installed' drop database XQueryIn10Minutes go create database XQueryIn10Minutes go use XQueryIn10Minutes go create table dbo.videos ( pkid tinyint identity(1,1) primary key, list xml ) go set quoted_identifier on go create primary xml index idxVideosList on dbo.videos(list) go insert into dbo.videos(list) values ('<genre><choice>action</choice><choice>comedy</choice><choice>drama</choice><choice>family</choice><choice>foreign</choice><choice>horror</choice><choice>musical</choice><choice>special</choice></genre><rating><choice>G</choice><choice>PG</choice><choice>PG-13</choice><choice>R</choice><choice>NC-17</choice></rating><user_rating><choice>1</choice><choice>2</choice><choice>3</choice><choice>4</choice><choice>5</choice></user_rating><summary/><details/><year/><director/><studio/><runtime/><vhs/><vhs_stock/><dvd/><dvd_stock/><beta/><beta_stock/><laserdisk/><laserdisk_stock/></video_template><actors><actor id="00000015">Anderson, Jeff</actor><actor id="00000030">Bishop, Kevin</actor><actor id="0000000f">Bonet, Lisa</actor><actor id="00000024">Bowz, Eddie</actor><actor id="0000002d">Curry, Tim</actor><actor id="00000033">Dullea, Keir</actor><actor id="00000042">Fisher, Carrie</actor><actor id="00000006">Ford, Harrison</actor><actor id="00000045">Foster, Jodie</actor><actor id="00000018">Ghigliotti, Marilyn</actor><actor id="0000000c">Hackman, Gene</actor><actor id="0000003f">Hamill, Mark</actor><actor id="00000027">Heames, Darin</actor><actor id="0000001e">Heche, Anne</actor><actor id="00000003">Jones, Tommy Lee</actor><actor id="00000039">Lockwood, Gary</actor><actor id="00000048">McConaughey, Matthew</actor><actor id="0000003c">Richter, Daniel</actor><actor id="00000021">Schwimmer, David</actor><actor id="00000009">Smith, Will</actor><actor id="0000001b">Spoonhauer, Lisa</actor><actor id="00000036">Sylvester, William</actor><actor id="0000002a">Todd, Antonio</actor><actor id="00000012">Voight, John</actor><actor id="0000004b">Woods, James</actor><actor id="1784027567">Lewis, Daniel</actor><actor id="2096814035">Manesse, Gaspard</actor><actor id="325442748">Feito, Raphael</actor><actor id="4231919377">Morier, Philippe</actor><actor id="2142583927">Racette, Francine</actor><actor id="916503204">Beatles, The</actor><actor id="916503205">Neeson, Liam</actor><actor id="916503206">Pinocchio, Mr.</actor><actor id="916503207">Parillaud, Anne</actor><actor id="916503208">Pitt, Brad</actor><actor id="916503209">Freeman, Morgan</actor><actor id="916503211">Domingo, Placido</actor><actor id="916503210">Sharif, Omar</actor></actors><videos><video id="id1235AA0"><title>The FugitiveactionPG-13Tommy Lee Jones and Harrison Ford are the hunter and the hunted in this fast-paced story of a falsely convicted man who escapes to find his wife''s true killer.
Harrison Ford and Tommy Lee Jones race through the breathless manhunt movie based on the classic TV series. Ford is prison escapee Dr. Richard Kimble, a Chicago surgeon falsely convicted of killing his wife and determined to prove his innocence by leading his pursuers to the one-armed man who actually commited the crime.
1997Andrew DavisWarner4110000000030000000613.9920614.991251.031212.0010
') go create procedure dbo.VideosWithActorsWithNamed ( @actor nvarchar(max) ) as begin set @actor = ' ' + @actor declare @n xml select @n=list.query(' for $video in (.//video) for $actor in (.//actor) where $video/actorRef/text() = $actor/@id and contains(($actor/text())[1],sql:variable("@actor")) order by ($video/title/text())[1] return {$video/title/text()}') from dbo.videos select al.a.value('.','varchar(max)') from @n.nodes('/v') al(a) end go create procedure dbo.VideosWithActorsWithNamedAsXML ( @actor nvarchar(max) = 'Lisa' ) as begin declare @n xml set @actor = ' ' + @actor select list.query(' for $video in (.//video),$actor in (.//actor) where $video/actorRef/text() = $actor/@id and contains(($actor/text())[1],sql:variable("@actor")) order by ($video/title/text())[1] return
  • {$video/title/text()}
  • ') from dbo.videos for xml path(''),root('ul'),type end go select 'The database has been successfully installed.'