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)
 CTE with XML issue

Author  Topic 

JosephBaggett
Starting Member

1 Post

Posted - 2008-12-14 : 09:23:42
I am doing a query that passes in xml data.

I have a parameter @DressCodeList xml.

The xml being passed in looks like this:

'<DressCodeList><DressCode><DressCodeId>1</DressCodeId></DressCode></DressCodeList>'

I declare the inside the stored proc this:

declare @xmlDressCodeDocument int
Exec sp_xml_preparedocument @xmlDressCodeDocument OUTPUT, @DressCodeList

I have a CTE I need to join with it

WITH DressCodeList
AS
(

SELECT DressCodeName,
DressCodeDescription
FROM DressCode
Inner JOIN OPENXML(@xmlDressCodeDocument,''DressCodeList/DressCode'', 2)
WITH(
xmlDressCodeId int ''./DressCodeId''
)As XmlDressCodeDocument
ON [DressCode].DressCodeId = [XmlDressCodeDocument].xmlDressCodeId
)

The problem is the CTE cannot allow the xml declaration at the top. What can I do?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-14 : 12:25:26
if you're using sql 2005 use xml functions like modify(),query(),value()... to parse the data.

something like

SELECT d.DressCodeName,
d.DressCodeDescription
FROM DressCode d
INNER JOIN
(
SELECT t.u.value('./DressCodeId[1]','int') AS DressCodeId
FROM @DressCodeList.nodes('/DressCodeList/DressCode')t(u)
)x
ON x.DressCodeId=d.DressCodeId
Go to Top of Page
   

- Advertisement -