I am currently working with User Defined Functions and having a hard time grasping the concept around how the function accepts one parameter and returns another one.
I have a table that has Employee ID and Log IDs. The UDF should accepts the EmployeeID and returns UserLoginID. The UserLoginID is the last part of the LogID column after the '\'. I need to return the entire column from the table.
Table Example
EmployeeID LoginId
1 database\userid1
2 database\userid2
3 database\userid3
UDF query 1:
CREATE FUNCTION dbo.fx_IDtest]
(@EmployeeID int)
RETURNS nvarchar(50)
AS
BEGIN
DECLARE @UserID nvarchar(50)
SET @UserID = (SELECT SUBSTRING(LoginID,CHARINDEX('\',LoginID)+1, LEN(LoginID))
FROM HumanResources.Employee
WHERE EmployeeID = @EmployeeID
)
RETURN @UserID
END
UDF query 2:
CREATE FUNCTION [dbo].[fx_IDtest2]
(@EmployeeID int)
RETURNS table
AS
RETURN
(SELECT SUBSTRING(LoginID,CHARINDEX('\',LoginID)+1, LEN(LoginID)) as USERID
FROM HumanResources.Employee
WHERE EmployeeID = @EmployeeID
)