/*
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 ('actioncomedydramafamilyforeignhorrormusicalspecialGPGPG-13RNC-1712345 Anderson, JeffBishop, KevinBonet, LisaBowz, EddieCurry, TimDullea, KeirFisher, CarrieFord, HarrisonFoster, JodieGhigliotti, MarilynHackman, GeneHamill, MarkHeames, DarinHeche, AnneJones, Tommy LeeLockwood, GaryMcConaughey, MatthewRichter, DanielSchwimmer, DavidSmith, WillSpoonhauer, LisaSylvester, WilliamTodd, AntonioVoight, JohnWoods, JamesLewis, DanielManesse, GaspardFeito, RaphaelMorier, PhilippeRacette, FrancineBeatles, TheNeeson, LiamPinocchio, Mr.Parillaud, AnnePitt, BradFreeman, MorganDomingo, PlacidoSharif, Omar')
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.'