I am creating a temp table from a query that generates data from SQL functions. I would like to use this temp table to perform an insert for each row of data from the temp table. What is the best way to proceed with this?
#tmpTable is as follows (not all columns shown for simplicity):
EmpID WorkHours HourlyEquivalent WeekOf
asmith 12 8.94 12/5/2011
bjones 23.5 12.75 12/5/2011
criley 40 7.89 12/5/2011
The table EmpHours will already be filled in with the employees hours but needs to be updated with their hourly equivalent (calculated from the aforementioned functions). I want to perform an UPDATE for each employee from the temp table as follows:
UPDATE EmpHours
SET HourlyEquivalent
WHERE WeekOf = #tmpTable.WeekOf
AND EmpID = #tmpTable.EmpID
The number of UPDATEs I would need to perform is under 50 if that matters.
SQL is much more efficient at doing operations like this in a single command, instead of a row-by-row update. Even though you don’t have very many rows in this scenario, it’s still a good habit to take a set based approach instead of using a loop or a cursor.