I try to join a list of ids from a xml stored in a column with ids from a column in another table. How can I extract them to get the matching rows.
I cant change the table. The schema can be reduced as:
CREATE TABLE abc([Data] [ntext] NOT NULL, [Group] varchar(3));
INSERT INTO abc
([Data], [Group])
VALUES
('<div>
<test name="Test1" enabled="True"/>
<ul>
<i><a id="t.foo"/></i>
<i><a id="t.bar"/></i>
</ul>
</div>', 'ADG')
;
GO
CREATE TABLE ForgeRock
([productName] varchar(13), [description] varchar(57), [Group] varchar(3))
INSERT INTO ForgeRock ([productName], [description], [Group])
VALUES
('foo', 'Platform for building enterprise provisioning solutions', 'ADG'),
('bar', 'Full-featured access management', 'ADG'),
('taz', 'Robust LDAP server for Java', 'ADG')
;
And here is the query I wrote:
SELECT
[productName]
, [description]
FROM ForgeRock
INNER JOIN abc on ForgeRock.[Group] = abc.[Group]
WHERE cast(abc.[Data] as xml).exist('//div/test[@name="Test1" and @enabled="True"]') = 1
AND ('t.' + productName) LIKE ('%' + cast(abc.[Data] as xml).value('(//ul/i/a/@id)[1]', 'varchar(50)') + '%')
I know that the problem come from the last line, but I am not sure how to tackle the problem.
The actual output is:
productName description
foo Platform for building enterprise provisioning solutions
But the expected output should be:
productName description
foo Platform for building enterprise provisioning solutions
bar Full-featured access management
I tried two solutions from SO (no they are not duplicated as far as I tried):
element, so you're getting what you asked for. To get the results you want you would need to also match on `LIKE ('%' + CAST(a.[Data] AS XML).value('(//ul/i/a/@id)[2]', 'varchar(50)') + '%'`
– Richard Hansell Oct 07 '20 at 14:48