/*
VersionID : 14		
RofstID   : 164	
RODBIDs	  : 2	RO	

RODBID Change from 2 to 1
*/
EXECUTE [vesp_TurnChangeManagerOFF] 
GO
-- Contents - replacing rodbid's in content records' link text, for the input version
update contents set text = replace(text, ' 2[END>', ' 1[END>')
where contentid in (
select vi.contentid from vefn_getversionitems('14') vi
join rousages ru on ru.contentid = vi.contentid
where ru.rodbid = 2)

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

-- ROUsages - updating rousage records with correct rodbid
update rousages set rodbid = 1
where rodbid = 2 and contentid in (
select vi.contentid from vefn_getversionitems('14') vi
join rousages ru on ru.contentid = vi.contentid
where ru.rodbid = 2)

-- DROUsages - updating rousage records with correct rodbid
update drousages set rodbid = 1
where rodbid = 2

delete from Figures where ROFstID = 164
update Associations set ROFstID = 160 where ROFstID = 164
delete from ROFsts where ROFstID = 164

-- ROFSTs - Update RODBID to 2 from 3
update rofsts set rodbid = 1 where rodbid = 2

-- Associations - No Change
-- ROImages - No Change
-- Figures - Insert duplicate records for figures
-- INSERT INTO [Figures] ([ROFstID],[ImageID],[Config],[DTS],[UserID])
--     select 24,[ImageID],[Config],[DTS],[UserID]  from [Figures] where ROFSTID = 23
-- Remove unused RODBs
Delete RODBs Where RODBID = 2
GO
EXECUTE [vesp_TurnChangeManagerON] 
GO


