我正在尝试创建一个表函数 Total_Salary_By_Dept,它返回一个包含 5 列的表 Total_Salary_By_Dept_Table:
Dept_No (Int)
Dept_Name (Char(30))
COUNT_Emp (INT)
SUM_Salary (INT)
AVE_Salary (INT)
全部带有公司数据库;
这是我到目前为止所拥有的,不确定我是否正确:
Use Company
GO
CREATE FUNCTION Total_Salary_By_Dept ()
RETURNS @Total_Salary_By_Dept_Table TABLE
(
Dept_No int,
Dept_name char(30),
COUNT_Emp int,
Sum_Salary int,
AVE_Salary int
)
AS
BEGIN
INSERT INTO @Total_Salary_By_Dept_Table
Select e.Dept_No, d.Dept_Name, Count(e.Dept_No), SUM(e.salary) as Sum_Salary,AVG(e.salary) as AVE_Salary
from EMPLOYEE e
join DEPARTMENT d ON e.dno = d.dnumber
Group by e.Dept_No, d.Dept_Name
RETURN
END
但我得到这个错误输出:
Msg 207, Level 16, State 1, Procedure Total_Salary_By_Dept, Line 18
Invalid column name 'Dept_No'.
Msg 207, Level 16, State 1, Procedure Total_Salary_By_Dept, Line 18
Invalid column name 'Dept_Name'.
Msg 207, Level 16, State 1, Procedure Total_Salary_By_Dept, Line 15
Invalid column name 'Dept_No'.
Msg 207, Level 16, State 1, Procedure Total_Salary_By_Dept, Line 15
Invalid column name 'Dept_Name'.
Msg 207, Level 16, State 1, Procedure Total_Salary_By_Dept, Line 15
Invalid column name 'Dept_No'.
我输入错了吗?我是否也以正确的方式完成了我的工作?请给我建议!