Moved the copy_sql_template line out of the loop