Re: OPENXML Sql 2000 to XQuery Sql 2005
- From: "Michael Rys [MSFT]" <mrys@xxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 11 Jul 2006 14:44:05 -0700
A small performance tip: moving the join into an exist() method is often
more performant:
SELECT E.* from #Employee E
CROSS APPLY @xdata.nodes('/offices/office') AS OS(O)
WHERE 1 = OS.O.exist('@id[.=sql:column("E.OfficeID")]')
Best regards
Michael
"Dan Sullivan" <danATpluralsight.com> wrote in message
news:964a9ae6fcd58c864ced429a2f6@xxxxxxxxxxxxxxxxxxxxx
Just to approximate your employee table:
CREATE TABLE #employee
(
id int IDENTITY PRIMARY KEY,
OfficeID INT
)
INSERT INTO #employee VALUES (1)
INSERT INTO #employee VALUES (2)
INSERT INTO #employee VALUES (2)
INSERT INTO #employee VALUES (1)
INSERT INTO #employee VALUES (3)
INSERT INTO #employee VALUES (3)
Then use OPENXML to have something to compare to:
declare @XmlDocument int
exec sp_xml_preparedocument @XmlDocument output, '<offices><office
id="1"/><office id="2"/></offices>'
-- instead of @XmlDocument, I want to have:
-- declare @Xml xml
-- select @Xml = ''
-- get employees from offices 1 and 2
select *
from #Employee E
join openxml(@XmlDocument, '//offices/office', 1) with (OfficeID int
'@id') OfficeXml on
E.OfficeID = OfficeXml.OfficeID
exec sp_xml_removedocument @XmlDocument
-- Then CROSS APPLY and nodes -- to pull office id's out of xml and join
-- with employee tables as was done with OPENXML
DECLARE @xdata xml
SET @xdata = '<offices><office id="1"/><office id="2"/></offices>'
SELECT E.* from #Employee E
CROSS APPLY @xdata.nodes('/offices/office') AS OS(O)
WHERE E.OfficeID = OS.O.value('@id', 'INT')
Dan
declare @XmlDocument int
exec sp_xml_preparedocument @XmlDocument output, '<offices><office
id="1"/><office id="2"/></offices>'
exec sp_xml_removedocument @XmlDocument
.
- Prev by Date: Re: Invalid XML Elements found inside cync block
- Next by Date: Re: Updategram Out of Memory! Do I need BulkLoad? How do I delete?
- Previous by thread: Re: Invalid XML Elements found inside cync block
- Next by thread: Re: Updategram Out of Memory! Do I need BulkLoad? How do I delete?
- Index(es):
Relevant Pages
|
Loading