且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用光标计算员工的总薪水

更新时间:2023-11-06 22:36:10

好的...使用你目前所拥有的东西只需移动显示循环内新细节的选择

Ok ... using what you have so far just move the select showing the new details inside the loop
while @@FETCH_STATUS=0
begin
    set @hradetails=@basicsalary*23/100
    set @totaldetails=@basicsalary + @hradetails
     -- display the results as you go along
select @empname as EmpName,@emplocation as EmpLocation,@deptname as Dept      ,@basicsalary as BasicSalary,@hradetails as HRA,@totaldetails as Total 
    fetch next from cur_sample4 into @empname,@emplocation,@deptname,@basicsalary,@hradetails,@totaldetails
end



请注意,它位于 fetch next 之前,以确保我们不要搞砸@@ FETCH_STATUS



另请注意,如果我这样做,我会将BasicSalary,HraDetails和TotalSalary声明为数字
并得到这样的结果...


Note that it is before the fetch next to ensure we don''t mess up @@FETCH_STATUS

Also note that if I was doing this I would have BasicSalary, HraDetails and TotalSalary declared as numeric
and would get the results like this ...

update #Employee_Details set HraDetails = BasicSalary * 23.0 / 100.0, TotalSalary = BasicSalary + (BasicSalary * 23.0 / 100.0)
select * from #Employee_Details


while @@ FETCH_STATUS = 0

begin

set @ hradetails = @ basicsalary * 23/100

set @ totaldetails = @ basicsalary + @hradetails

从cur_sample4获取下一个到@empname,@ emplocation,@ deptname,@ basicsalary,@ hradetails,@ littledetails

end



在以下循环中你只计算当前行的数据





选择@empname作为EmpName,@ emplocation作为EmpLocation,@ deptname作为Dept

,@ basicsalary作为BasicSalary, @hradetails作为HRA,@ totaldetails作为总计

以上行您选择的最后一个记录





使用以下查询来选择计算数据或更新

SELECT emp.EmpName,emp.EmpLocation,dep.DeptName,emp.BasicSalary,

(emp.BasicSalary * 23/100)AS HraDetails,(emp.BasicSalary,(emp.BasicSalary * 23/100) ))AS emp.TotalSalary

FROM Employee_Details emp

INNER JOIN Department_Details dep

ON emp.EmpId = dep.EmpId



用于更新Hra和总薪水

UPDATE emp SET HraDetails =(BasicSalary * 23/100),TotalSalary =(BasicSalary +(BasicSalary * 23/100) )

FROM Employee_Details emp

INNER JOIN Department_Details dep

ON emp.EmpId = dep.EmpId
while @@FETCH_STATUS=0
begin
set @hradetails=@basicsalary*23/100
set @totaldetails=@basicsalary + @hradetails
fetch next from cur_sample4 into @empname, @emplocation, @deptname, @basicsalary, @hradetails, @totaldetails
end

in following loop you are only calculating the data for current row


select @empname as EmpName,@emplocation as EmpLocation,@deptname as Dept
,@basicsalary as BasicSalary,@hradetails as HRA,@totaldetails as Total
in above line your selecting the last fectched record


use following queries to select calculated data or updating
SELECT emp.EmpName, emp.EmpLocation, dep.DeptName, emp.BasicSalary,
(emp.BasicSalary * 23 / 100) AS HraDetails, (emp.BasicSalary, (emp.BasicSalary * 23 / 100)) AS emp.TotalSalary
FROM Employee_Details emp
INNER JOIN Department_Details dep
ON emp.EmpId = dep.EmpId

For updating Hra and total salary
UPDATE emp SET HraDetails = (BasicSalary * 23 / 100), TotalSalary = (BasicSalary + (BasicSalary * 23 / 100))
FROM Employee_Details emp
INNER JOIN Department_Details dep
ON emp.EmpId = dep.EmpId