I have two tables that I want to use in my query, tblEmployee and tblEmpPerformance
The requirement is:
Using tblEmployee.EmpID, tblEmployee.ManagerID, go thru tblEmpPerformance and look at tblEmpPerformance.SalaryRaise and tblEmpPerformance.CommissionRaise, and update tblEmployee accordingly.
Only one of these two fields will have a non-zero value for every record in tblEmpPerformance
if tblEmployee.EmpID = tblEmpPerformance.EmpID then
If tblEmpPerformance.Salary > 0 then
update tblEmployee
Set CompensationType = 'Salary'
Where tblEmployee.EmpID = tblEmpPerformance.EmpID
AND tblEmployee.ManagerID = tblEmpPerformance.ManagerID
update tblEmployee
Set SalaryRaise = tblEmpPerformance.SalaryRaise
Where tblEmployee.EmpID = tblEmpPerformance.EmpID
AND tblEmployee.ManagerID = tblEmpPerformance.ManagerID
If tblEmpPerformance.Commission > 0 then
update tblEmployee
Set CompensationType = 'Commission'
Where tblEmployee.EmpID = tblEmpPerformance.EmpID
AND tblEmployee.ManagerID = tblEmpPerformance.ManagerID
update tblEmployee
Set CommissionRaise = tblEmpPerformance.CommissionRaise
Where tblEmployee.EmpID = tblEmpPerformance.EmpID
AND tblEmployee.ManagerID = tblEmpPerformance.ManagerID
If no matching EmpID‘s found, then look in tblContractorPerformance
if tblEmployee.EmpID = tblContractorPerformance.EmpID then
If tblContractorPerformance.Salary > 0 then
update tblEmployee
Set CompensationType = 'Salary'
Where tblEmployee.EmpID = tblContractorPerformance.EmpID
AND tblEmployee.ManagerID = tblContractorPerformance.ManagerID
....
....
....
....
Next tblEmployee.EmpID
Update:
DOH!!..
What would be the best way to write this SQL query. Stored Proc or cursor?
Any suggesstion on how to design the query would be a great help as well
Please avoid cursors. Think set-based in dealing with your data. So execute on your first set of data, where
tblEmpPerformance.Salary > 0. Do your updates, then do the second operation on the next set of data wheretblEmpPerformance.Commission > 0. Lastly, update all the data where thetblContractorPerformance.Salary > 0.Put these operations in a stored procedure and I’m confident you’ll see better performance than the equivalent using cursors.