SQL: Sorted out temporary names and added missing statements