
This normally requires manually setting the Time Machine preferences every time you want to switch drives. So I put together an AppleScript to do this, and launch it automatically when my external drive is mounted or unmounted (using the free utility Do Something When).
The script uses GUI Scripting to set Time Machine to backup to a "primary drive" (the primarydrivename variable in the script) if it is available, and if it isn't, it will attempt to have it backup to a "secondary drive" (secondarydrivename). Failing that, it leaves the Time Machine preferences unchanged.
[robg adds: I haven't tested this one.]