I have a database that looks like :
http://sqlfiddle.com/#!2/d379d/1
CREATE TABLE content (contentid int, version int, versioncomment varchar(512), prevver int);
insert into content values (101,1, null, null);
insert into content values (102,2, null, 101);
insert into content values (100,3, 'Migrated', 102);
insert into content values (201,1, null, null);
insert into content values (202,2, null, 201);
insert into content values (200,3, 'Migrated', 202);
insert into content values (301,1, null, null);
insert into content values (302,2, null, 301);
insert into content values (300,3, '', 302);
CREATE TABLE os_prop (entity_id int, entity_key varchar(512), text_val varchar (512));
insert into os_prop values(100, 'My.Key','value100');
insert into os_prop values(101, 'My.Key','value101');
insert into os_prop values (102, 'My.Key',null); <====== PROBLEM !
insert into os_prop values(101, 'Random.Key','randomValue101');
insert into os_prop values (100, 'Random.Key','randomValue100');
insert into os_prop values(200, 'My.Key','value200');
insert into os_prop values(201, 'My.Key','value201');
insert into os_prop values(202, 'My.Key',null); <====== PROBLEM !
insert into os_prop values(201, 'Random.Key','randomValue201');
insert into os_prop values(200, 'Random.Key','randomValue200');
// no problem here as nothing has been migrated. leave it as is
insert into os_prop values(300, 'My.Key','value300');
insert into os_prop values(301, 'My.Key','value301');
insert into os_prop values(302, 'My.Key','value302');
and I need it to look like:
http://sqlfiddle.com/#!4/21151/1
CREATE TABLE content (contentid int, version int, versioncomment varchar(512), prevver int);
insert into content values (101,1, null, null);
insert into content values (102,2, null, 101);
insert into content values (100,3, 'Migrated', 102);
insert into content values (201,1, null, null);
insert into content values (202,2, null, 201);
insert into content values (200,3, 'Migrated', 202);
insert into content values (301,1, null, null);
insert into content values (302,2, null, 301);
insert into content values (300,3, '', 302);
CREATE TABLE os_prop (entity_id int, entity_key varchar(512), text_val varchar (512));
insert into os_prop values(100, 'My.Key','value100');
insert into os_prop values(101, 'My.Key','value101');
insert into os_prop values (102, 'My.Key','value100'); <===== FIXED
insert into os_prop values(101, 'Random.Key','randomValue101');
insert into os_prop values (100, 'Random.Key','randomValue100');
insert into os_prop values(200, 'My.Key','value200');
insert into os_prop values(201, 'My.Key','value201');
insert into os_prop values(202, 'My.Key','value200'); <===== FIXED
insert into os_prop values(201, 'Random.Key','randomValue201');
insert into os_prop values(200, 'Random.Key','randomValue200');
// untouched
insert into os_prop values(300, 'My.Key','value300');
insert into os_prop values(301, 'My.Key','value301');
insert into os_prop values(302, 'My.Key','value302');
The examples above is simplified to distill my question but in reality we have a database that contains thousands of contentids with multiple versions or perhaps only 1 version (some migrated and some not migrated). Therefore this has to be done with a PL/SQL script (perhaps using cursors).
In a nutshell when a database migration occurs we have these "My.Key" values in os_prop table being lost for all previous versions of a migrated contentid. So the goal is to copy back the "My.Key" value of the migrated content to it's previous version. A null value for "My.Key" value is unacceptable.
So I'm not an expert a PL/SQL but I know I need to do something like this:
// step 1 find list of contentids that have the word "Migrated" in their comment:
contentids = select contentids c, version v, prevver pv from content where versioncomment like '%Migrated%';
// step 2 for each contentid listed in step 1 find its corresponding "My.Key" value from os_prop
loop
select text_val from os_prop where entity_id = <contentid from step 1>
// step 3 need to figure out the previous version's contentID of each "Migrated" content found in step 1
if pv is null
select contentid where prevver = c and version = v-1
else
select contentid where prevver=pv and version = v-1
// step 4 finally need to update text_val to the same value as migrated version's
update os_prop set text_val = <text_vals from step 2>where entity_id = <prev version from step 3>