I have an XML field inside a SQL Server table, on which I need to know if a couple key/value with a given key and a given value is already used in my table.
So here is my table (simplified).
dbo.mytable :
primaryKey, INT
xml_data, NOT NULL
And the xml_data field is caracterized with the following XSN :
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xsd:element name="a">
<xsd:complexType>
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:element name="b" type="NonEmptyString" />
<xsd:element name="c" type="NonEmptyString" />
<xsd:element name="d" type="NonEmptyString" />
<xsd:element name="e" type="xsd:dateTime" />
<xsd:element name="dict">
<xsd:complexType>
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:choice maxOccurs="unbounded">
<xsd:sequence>
<xsd:element name="key" type="NonEmptyString" />
<xsd:element name="value" type="xsd:string" />
</xsd:sequence>
</xsd:choice>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="version" type="xsd:float" />
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="NonEmptyString">
<xsd:restriction base="xsd:string">
<xsd:minLength value="1" />
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
For this given XML (for example) :
<a>
<b>Value</b>
<c>Value</c>
<d>Value</d>
<e>2017-02-14T00:00:00</e>
<dict>
<key>myKey</key>
<value>myValue</value>
<key>anotherKey</key>
<value>myValue</value>
</dict>
</a>
I need to know if the table contains for the first key "myKey" the value "myValue".
I think I can use the [xml_data].exist()
function, but I have issues about how my XQuery should be formed.
I started writing the following XQuery :
for $key in /a/dict/key where data($key) = ''myKey'' return $key
But I can't find out, how to get the following tag.
Which could give "hypotethically" something like :
SELECT [xml_data].exist('
for $key
in /a/dict/key
where data($key) = ''myKey''
return $key.followingTag == ''myValue''
') FROM dbo.mytable;
References : https://msdn.microsoft.com/en-us/library/ms189869.aspx
Considering the following answer : https://stackoverflow.com/a/19253986/3635715
And the following documentation : https://msdn.microsoft.com/en-us/library/ms178030.aspx
Here is the XQuery for my problem :
data(/a/dict/value[. >> (/a/dict/key[. = "myKey"])[1]])[1]
Here is the answer for my problem :
SELECT COUNT(primaryKey) FROM dbo.mytable
WHERE [xml_data].value('
data(/a/dict/value[. >> (/a/dict/key[. = "myKey"])[1]])[1]
', 'varchar(max)') = 'myValue'
Be careful this answer only works for first key corresponding to "myKey" found.
Which for me will be the case.
Also considering this Answer : https://stackoverflow.com/a/10408858/3635715
The query can be parameterised this way :
DECLARE @key AS NVARCHAR(max)
DECLARE @value AS NVARCHAR(max)
SET @key = N'myKey'
SET @value = N'myValue'
SELECT COUNT(primaryKey) FROM dbo.mytable
WHERE [xml_data].value('
data(/a/dict/value[. >> (/a/dict/key[. = sql:variable("@key")])[1]])[1]', 'varchar(max)
') = @value