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)
 XML Column values comparisons.

Author  Topic 

Jawad
Starting Member

17 Posts

Posted - 2012-04-24 : 07:57:47
XML column search

I have XML column ‘ColXML’ with following values in separate rows.


<values>
<valueid>210</valueid>
<valueid>214</valueid>
</values>
----------------------
<values>
<valueid>11</valueid>
<valueid>34</valueid>
</values>
----------------------
<values>
<valueid>67</valueid>
<valueid>66</valueid>
</values>
----------------------
<values>
<valueid>210</valueid>
<valueid>214</valueid>
<valueid>215</valueid>
</values>
----------------------

I wish to create a query that compare the XML column with input and provide the row that match the input comparisons values in ‘ColXML’

Some think like following
Select * from mytable where ColXML in (210,214,215) will return last row.
And
Select * from mytable where ColXML in (210,214) will return the 1st row.

Wishes

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-04-24 : 09:01:15
You can't really compare an XML column, so you will have to query it to see if the data matches the input. When you query, how you would do it depends on the details of your requirements and data - for example,

a) is order important? (i.e., is 210,214,215 the same as 210,215,214)?

b) are you only able to get the input in comma-separated form, or are you able to get it in tabular form?
Go to Top of Page

Jawad
Starting Member

17 Posts

Posted - 2012-04-24 : 09:34:33
quote:
Originally posted by sunitabeck

You can't really compare an XML column, so you will have to query it to see if the data matches the input. When you query, how you would do it depends on the details of your requirements and data - for example,

a) is order important? (i.e., is 210,214,215 the same as 210,215,214)?

b) are you only able to get the input in comma-separated form, or are you able to get it in tabular form?



Hi sunitabeck

Thanks for reply.
a) No (order not important)
b) comma-separated only
any luck for me?
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-04-24 : 10:23:58
I don't have anything that I would call good. The following code, with sample works, but a few caveats.

a) You need a string-splitter function. Go to http://www.sqlservercentral.com/articles/Tally+Table/72993/ copy the code in Fig 21 on that blog and install on your database.

b) As you can see, this is not a simple select statement. It is rather convoluted. There may be a simpler way, but it escapes me.

In any case, here is the code:
---------------------------------------------------------------------------------
-- TEST DATA
CREATE TABLE #tmp(x XML);
INSERT INTO #tmp VALUES (
'<values>
<valueid>210</valueid>
<valueid>214</valueid>
</values>'),
('<values>
<valueid>11</valueid>
<valueid>34</valueid>
</values>'),
('<values>
<valueid>67</valueid>
<valueid>66</valueid>
</values>'),
('<values>
<valueid>210</valueid>
<valueid>214</valueid>
<valueid>215</valueid>
</values>');

---------------------------------------------------------------------------------
-- Comma-separated input string.
DECLARE @var VARCHAR(32); SET @var = '210,214,215';

-- query
;WITH cte1 AS
(
SELECT x,ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RN
FROM #tmp
),
cte2 AS
(
SELECT
c1.RN,
ROW_NUMBER() OVER (PARTITION BY RN ORDER BY (SELECT NULL)) AS RN2,
c1.x,
CAST(c.query('data(.)') AS VARCHAR(32)) val
FROM
cte1 c1
CROSS APPLY x.nodes('//valueid') T(c)
)
SELECT
x
FROM
cte2 cc1
WHERE
NOT EXISTS
(
SELECT val FROM cte2 cc2 WHERE cc1.RN = cc2.RN
EXCEPT
SELECT Item from dbo.DelimitedSplit8K(@var,',')
)
AND
NOT EXISTS
(
SELECT Item from dbo.DelimitedSplit8K(@var,',')
EXCEPT
SELECT val FROM cte2 cc2 WHERE cc1.RN = cc2.RN
)
AND RN2 = 1;
---------------------------------------------------------------------------------

DROP TABLE #tmp;
Go to Top of Page
   

- Advertisement -