If the condition is so complicated that you don't want to execute it twice (which BTW sounds unlikely to me, but anyway), one possibility would be to ALTER TABLE ... ADD COLUMN
on the original table to add a boolean field, and run an UPDATE
on the table to set that field to true WHERE <condition>
. Then your INSERT
and DELETE
commands can simply check this column for their WHERE
clauses.
Don't forget to delete the column from both source and destination tables afterwards!
Hmm, even less intrusive would be to create a new temporary table whose only purpose is to contain the PKs of records that you want to include. First INSERT
to this table to "define" the set of rows to operate on, and then join with this table for the table-copying INSERT
and DELETE
. These joins will be fast since table PKs are indexed.
[EDIT]
Scott Bailey's suggestion in the comments is obviously the right way to do this, wish I'd thought of it myself! Assuming all the original table's PK fields will be present in the destination table, there's no need for a temporary table -- just use the complex WHERE
conditions to insert into the destination, then DELETE
from the original table by joining to this table. I feel stupid for suggesting a separate table now! :)