-- replacing rodbid's in content records' link text, for the input version
update contents set text = replace(text, ' 4[END>', ' 6[END>')
where contentid in (
select vi.contentid from vefn_getversionitems('22,25,26') vi
join rousages ru on ru.contentid = vi.contentid
where ru.rodbid = 4)

-- replacing rodbid's in grid records' link text
update grids set data = cast(replace(cast(data as nvarchar(max)), ' 4[END&gt;', ' 6[END&gt;') as xml)
where contentid in (
select vi.contentid from vefn_getversionitems('22,25,26') vi
join rousages ru on ru.contentid = vi.contentid
where ru.rodbid = 4)

-- updating rousage records with correct rodbid
update rousages set rodbid = 6
where rodbid = 4 and contentid in (
select vi.contentid from vefn_getversionitems('22,25,26') vi
join rousages ru on ru.contentid = vi.contentid
where ru.rodbid = 4)