The beginning of knowledge is the discovery of something we do not understand. [Frank Herbert]

Menu

This is something I knew somewhere in the back of my head, but had forgotten about until now.

When you want to update multiple columns in a single SQL statement based on a sub query you can of course duplicate this query for every column you want to update. But this violates the SPOD (Single Point Of Definition) ‘rule’.

As an example I have the following requirement:

Add two columns to the EMP table containing the name and job of the manager and fill these columns with the right values.

First of all I need to add the columns to the table, with is easy:

altertable emp add (mgr_name varchar2(10)
,mgr_job varchar2(9)
)

Then comes the ‘tricky’ part. I can of course fill up these columns in separate statements, like this:

This is of course a pretty simple example, but you can imagine what would happen if you want to update more columns, create a complex sub query or worse, make modifications to the predicates. You are more than likely going to forget one or more sub queries giving you an undesired result.