문제

I have a table with one column:

IF OBJECT_ID('tempdb..#tmp') IS NOT NULL DROP TABLE #tmp
CREATE TABLE #tmp
(
data XML
)GO

with one record:

INSERT INTO #tmp
VALUES ( N'<RelevanExpertXML><Tel><RelevanExpert>1</RelevanExpert></Tel><Tel><RelevanExpert>2</RelevanExpert></Tel></RelevanExpertXML>')

and another tale with one column

CREATE TABLE #tmp2
(
id int
)
GO

and i want to write this query:

select * 
from #temp
where xml.exist('/RelevanExpertXML/Tel/RelevanExpert[(text()) = [select id from     #temp2]]') = 1

in fact i want to write sub query in exist(), but i get error, and also i can't change from clause and select list, only i can change where clause.

thanks for help.

도움이 되었습니까?

해결책

you can use this query:

select * 
from #tmp
where exists
    (
       select * 
       from #tmp2 as t
       where
           #tmp.data.exist('/RelevanExpertXML/Tel/RelevanExpert[(text()) = sql:column("t.id")]') = 1
    )

but it will return you the whole xml. If you want to split xml by rows, you have to use nodes() function in the from clause.

다른 팁

You can write

select * 
from #tmp, #temp2
where data.exist('/RelevanExpertXML/Tel/RelevanExpert[text()] = sql:column("id")')=1

But I don't think it will give you the results you're after.

You may be after something more like

select t.r.value('.','int')
from #tmp temp
cross apply data.nodes('/RelevanExpertXML/Tel/RelevanExpert') t(r)
inner join #temp2 t2 on t.r.value('.','int') = t2.id
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top