Subqueries and Correlated Updates
Subqueries and correlated updates can be performed on
RWDBUpdater in a similar fashion to
RWDBSelector. Please refer to
Subqueries for the details of constructing subqueries and correlated subqueries.
Following is an example of use of a subquery in an RWDBUpdater.
RWDBTable primary = myDbase.table("primary");
RWDBTable backup = myDbase.table("backup");
RWDBSelector avg = myDbase.selector(); //1
avg << rwdbAvg(backup["onHand"]);
RWDBUpdater update = primary.updater(); //2
update << primary["notes"].assign("Note A"); //3
update.where(primary["onHand"] >= avg); //4
A reasonable interpretation of this code fragment could be expressed this way in SQL:
UPDATE primary
SET primary.notes = 'Note A'
WHERE primary.onHand >=
(
SELECT AVG(backup.onHand)
FROM backup
)
Following is an example of correlated update by use of a correlated subquery in an
RWDBUpdater.
RWDBTable primary = myDbase.table("primary");
RWDBTable backup = myDbase.table("backup");
RWDBSelector avg = myDbase.selector(); //1
avg << rwdbAvg(backup["onHand"]);
avg.where(backup["ID"] == primary["ID"]);
avg.fromExtern(primary);
RWDBUpdater update = primary.updater(); //2
update << primary["notes"].assign("Note B");
update.where(primary["onHand"] >= avg);
A reasonable interpretation of this code fragment could be expressed this way in SQL:
UPDATE primary
SET primary.notes = 'Note B'
WHERE primary.onHand >=
(
SELECT AVG(backup.onHand)
FROM backup
WHERE backup.ID = primary.ID
)