There is a lot of duplication, probably a template for the "copy" SQL query is a good idea.