CREATE FUNCTION Query2_Function(@DPT INT)
RETURNS @tbl TABLE
(
departmentid int ,
[name] varchar(100),
cnt int
)
AS
begin
IF @DPT is not null
insert into @tbl (departmentid,name,cnt)
select edh.departmentid,d.name,count(*)as cnt
From HumanResources.Employee e
inner join HumanResources.EmployeeDepartmentHistory edh on e.employeeID = edh.employeeid
inner join humanresources.department d on edh.departmentid = d.departmentid
where d.DepartmentID =@DPT
group by edh.departmentid, d.name
return
end
GO