Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
i found solution to use top 1 in the query but it only return one top value. I know variable store only one value but i cant find any other way to do.
Here is my Stored Procedure
Please Help.
SQL
Table 1: dbo.tblEmpDetail (Name varchar(30), BasicPay int, ScaleID int)

SQL
Table 2: dbo.tblAllowance (Name varchar(30), Amount int, ScaleID int)

SQL
Table 3: dbo.tblDeduction (Name varchar(30), Amount int, ScaleID int)



SQL
  alter proc Test
as
begin
/*Tax cal variable*/
/*AllowanceDeduction cal vairable*/
declare @totaldeduc int,
 @totalAllowance int,
 @grosspay int
set @totaldeduc = ( select sum(Amount) from tblAllowance A
					   inner join tblEmpOfficialDetail O on O.ScaleID=A.ScaleID
					   group by A.ScaleID, O.EmpID) 
set @totalAllowance= ( select sum(Amount) from tblDeduction A
						  inner join tblEmpOfficialDetail O on O.ScaleID=A.ScaleID
						  group by A.ScaleID, O.EmpID)
set @grosspay= (select O.BasicPay from tblEmpOfficialDetail O 
					inner join tblAllowance A on A.ScaleID=O.ScaleID
					inner join tblDeduction D on D.ScaleID=O.ScaleID
					group by o.ScaleID,o.EmpID, O.BasicPay )+@totaldeduc-@totalAllowance
select @grosspay
end
Execute Test


What I have tried:

I am trying to calculate the gross pay of all employees by adding the allowances [dbo.tblAllowances] and subtracting the deductions [dbo.Deductions] against specific rank of employee from basic pay [dbo.EmpDetails] of the employee.
Posted
Updated 4-Aug-16 0:26am
v3
Comments
Suvendu Shekhar Giri 11-Jul-16 9:04am    
The error is meaningful and it works that way for a reason. What exactly your requirement is?
Please expalin a little more about your requirement and we'll try to suggest an appropriate apprach.
Ribha Shakoor 11-Jul-16 11:03am    
Check it now please

1 solution

Use Temporary tables to achieve your goal some other method also available but this is easy..

SQL
Create Table #Temp
(
Sn                      INT IDENTITY,
EmpId                   int,
Name                    Varchar(150),
totaldeduc              decimal(10,2),
totalAllowance          decimal(10,2),
basicpay                decimal(10,2),
grosspay                decimal(10,2)
)
insert into #temp(EmpId)
select EmpId from tblEmpDetail

Update #temp set
totaldeduc=(select sum(Amount) from tblAllowance where EmpId=#temp.EmpId),
totalAllowance=(select sum(Amount) from tblDeduction where EmpId=#temp.EmpId),
basicpay=(select BasicPay from tblEmpOfficialDetail where EmpId=#temp.EmpId)

Update #Temp set grosspay=(basicpay+totalAllowance-totaldeduc)

Select * from #Temp
Drop table #Temp



I suppose in all your table EmpId is present.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900