简体   繁体   English

用户定义的SQL函数给出错误“无效的列名'Employee_Name'。”

[英]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.

相关问题 用户定义的函数 - Northwind数据库 - Employee_Name函数 - User Defined Functions - Northwind Database - Employee_Name function 找不到列或用户定义的函数或聚合,或者名称不明确 - cannot find column or the user-defined function or aggregate, or the name is ambigous PL/Python 错误:NameError:未定义全局名称“用户定义函数的名称” - PL/Python ERROR: NameError: global name 'name of user-defined function' is not defined 解决SQL异常找不到列“ partinfo”或用户定义的函数或聚合“ partinfo.query”,或者名称不明确 - to solve sql exception Cannot find either column “partinfo” or the user-defined function or aggregate “partinfo.query”, or the name is ambiguous MS SQL (SSMS) - 找不到列“dbo”或用户定义的 function 或聚合“dbo.CheckEmail”,或者名称不明确 - MS SQL (SSMS)- Cannot find either column "dbo" or the user-defined function or aggregate "dbo.CheckEmail", or the name is ambiguous BigQuery中的用户定义函数使用带点的列名 - User-defined functions in BigQuery used column name with dot 如何在SELECT语句中以列名作为参数调用用户定义的函数? - How to call User-defined function in SELECT statement with Column Name as parameter? 找不到列“ dbo”或用户定义的函数或聚合“ dbo.GetUserBranchIDs”,或者名称不明确 - Cannot find either column “dbo” or the user-defined function or aggregate “dbo.GetUserBranchIDs”, or the name is ambiguous 插入/更新过程给出无效的列名错误SQL Server - Insert/ Update Procedure gives Invalid Column Name Error SQL Server SQL别名提供无效的列名 - SQL alias gives invalid column name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM