select id, count(*)
from (
select x.value('@id', 'varchar(100)') as id
from @x.nodes('//*[@id]') t(x)) as ids
group by id
having count(*) > 1;
关键是xpath
//*[@id]
选择具有
id
属性。
更新
如果XML在表字段中:
select id, count(*)
from (
select x.value('@id', 'varchar(100)') as id
from [<table>]
cross apply [<table>].[<field>].nodes('//*[@id]') t(x)) as ids
group by id
having count(*) > 1;
这将选择
全部的
排。如果只想在每行中选择重复的属性值,请将表主键添加到分组依据:
select id, <primary key>, count(*)
from (
select x.value('@id', 'varchar(100)') as id
, <primary key>
from [<table>]
cross apply [<table>].[<field>].nodes('//*[@id]') t(x)) as ids
group by id, <primary key>
having count(*) > 1;