select distinct vn.*,t1.contentid from (
select gg.*, len(cast (data as nvarchar(max))) - len(replace( cast (data as nvarchar(max)),':ReferencedObject:',':ReferencedObject')) ros from grids gg
join ROUsages ru on gg.ContentID = ru.ContentID where cast (data as nvarchar( max)) not like '%:' + cast (rousageid as nvarchar(10)) + ' %') T1
join vefn_GetVersionItems('') vi on vi.ContentID = t1.ContentID
join vefn_GetVersionNames() vn on vn.VersionID = vi.VersionID
where ros > 0