[英]user-Defined SQL Function gives error “Invalid column name 'Employee_Name'.”
I am working one something and I can't get it to work. 我正在做某事,但无法正常工作。 I am trying to Create a user defined function named Employee_Name.
我正在尝试创建一个名为Employee_Name的用户定义函数。 Pass an Employee Id as a parameter.Return the First Name, Last Name, and Phone Number of the employee whose ID matches the id passed to the user defined function.Create a query that uses the function and returns information for the employee with an id of 5. But In order to finish the function employee name I can not get it to work.
传递一个员工ID作为参数,返回其ID与传递给用户定义函数的ID匹配的员工的名字,姓氏和电话号码创建一个使用该函数并返回带有ID的员工信息的查询之5。但是为了完成职能员工姓名,我无法使用它。 I keep getting this error:
我不断收到此错误:
Msg 207, Level 16, State 1, Procedure Employee_Name, Line 10
Invalid column name 'Employee_Name'.
Msg 208, Level 16, State 1, Line 2
Invalid object name 'Employee_name'.
The code looks like this: 代码如下:
CREATE FUNCTION Employee_Name
(
@EmployeeID int
)
RETURNS TABLE
AS
RETURN
(
SELECT * FROM Employees
WHERE Employee_Name = @EmployeeId
)
GO
SELECT * FROM Employee_name (1)
Now to get the Return on First name,lastname and phone wouldnt I so a select on the employees id where it goes like this 现在,要获得“名字,姓氏和电话”的回报,我就这样选择员工编号,就像这样
Select
FirstName,
LastName,
PhoneNumber
From EmployeeId
Or something like that.Now to get the employee with and Id of 5 would i use the count or do something that has an =5; 或类似的东西。现在让员工的ID为5,我将使用计数还是做一个= 5的事情? I am not to sure still confused.
我不确定是否仍然感到困惑。
Edit: This is what I have again: 编辑:这是我再次:
CREATE FUNCTION Employee_Name
(
@EmployeeID int
)
RETURNS TABLE
AS
RETURN
(
SELECT FirstName,LastName,HomePhone
FROM Employees
WHERE Employee_Name = @EmployeeId
)
GO
WHERE Employee_Name = @EmployeeId <<<-
should that be 应该是
WHERE Employee_Id = @EmployeeId
? ? (or whatever your Id column is named).
(或您的ID列命名的任何名称)。
That's probably because function has the same name as the column. 这可能是因为函数与列具有相同的名称。
Try this: 尝试这个:
SELECT * FROM Employees
WHERE Employees.Employee_Name = @EmployeeId
or rather 更确切地说
SELECT * FROM Employees
WHERE Employees.EmployeeId = @EmployeeId
This line: 这行:
WHERE Employee_Name = @EmployeeId
does not make sense. 没有道理。 Surely you mean something like
当然你的意思是
WHERE Employees.EmployeeId = @EmployeeId
? ?
And this query: 而这个查询:
Select
FirstName,
LastName,
PhoneNumber
From EmployeeId
should surely be this: 应该肯定是这样的:
SELECT FirstName,
LastName,
PhoneNumber
FROM Employees
WHERE EmployeeId = ...
? ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.