UDF to calculate the total salary paid by input department

Bandi
Posted by Bandi under Sql Server category on | Points: 40 | Views : 511
CREATE FUNCTION dbo.fn_TotalSal ( @p_deptid INT)
RETURNS FLOAT
BEGIN
DECLARE @v_tot_sal FLOAT = 0.0;
SELECT @v_tot_sal = SUM(salary)
FROM employees
WHERE department_id = @p_deptid

RETURN @v_tot_sal
END
GO

--calling above function in different ways
SELECT dbo.fn_TotalSal(50)
GO
SELECT dbo.fn_TotalSal(department_id) FROM departments
GO
SELECT distinct dbo.fn_TotalSal(department_id) FROM employees
GO

Comments or Responses

Login to post response