Wednesday, March 7, 2012

call function inside Stored Procedure Error

i call the function inside the stored procedure and when i execute it theres a error

ALTERPROCEDURE [dbo].[usp_LoadEmployees]

-- Add the parameters for the stored procedure here

AS

BEGIN

SELECT dbo.Employees.EmployeeID,dbo.fn_ProperEmployees(dbo.Employees.EmployeeID)as MyEmployee,

dbo.Employees.Age, dbo.Employees.Sex, dbo.Employees.PositionID, dbo.Positions.Position_Name, dbo.Employees.DepartmentID,

dbo.Departments.DepartmentName

FROM dbo.Employees INNERJOIN

dbo.Departments ON dbo.Employees.DepartmentID = dbo.Departments.DepartmentID INNERJOIN

dbo.Positions ON dbo.Employees.PositionID = dbo.Positions.PositionID

GROUPBY dbo.Employees.EmployeeID, dbo.Employees.MI,

dbo.Employees.Age, dbo.Employees.Sex, dbo.Employees.PositionID, dbo.Positions.Position_Name, dbo.Employees.DepartmentID,

dbo.Departments.DepartmentName

ORDERBY dbo.Employees.EmployeeID

END

- function is this -

ALTER FUNCTION [dbo].[fn_ProperEmployees](@.cEmployeeID varchar(50))

RETURNS TABLE

AS

RETURN

(

-- Add the SELECT statement with parameter references here

SELECT EmployeeName =(Upper(dbo.Employees.Lastname)+','+ dbo.Employees.Firstname +' '+ dbo.Employees.MI)

FROM Employees

WHERE EmployeeID = @.cEmployeeID

)

the error is

Cannot find either column "dbo" or the user-defined function or aggregate "dbo.fn_ProperEmployees", or the name is ambiguous.

The function is a inline table function, if it is table function then you can't use it as column (only scalar functions are allowed)..

Change your function as follow as ,

Code Snippet

ALTER FUNCTION [dbo].[fn_ProperEmployees](@.cEmployeeID varchar(50))

RETURNS Varchar(1000)

AS

Begin

Declare @.EmployeeName as Varchar(1000);

-- Add the SELECT statement with parameter references here

SELECT

@.EmployeeName = (Upper(dbo.Employees.Lastname)

+ ',' + dbo.Employees.Firstname

+ ' ' + dbo.Employees.MI)

FROM

Employees

WHERE

EmployeeID = @.cEmployeeID;

Return @.EmployeeName;

End

No comments:

Post a Comment