Fred
asked on
Creating a user defined function
I am lost in trying to create a user defined function. I want to understand what am doing
output that I currently have
LoginID (adventure-works\alan0 ) UserLogintID (alan0) from dbo.HumanResources.Employe e
1. Create a UDF that accepts EmployeeID (2012: BusinessEntityID) and returns UserLoginID.
The UserLoginID is the last part of the LoginID column. It’s only the part that comes after the \
select LoginId
,SUBSTRING(LoginID,CHARIND EX('\', LoginID,0) + 1, len(LoginId)) AS UserLoginID
from [HumanResources].[Employee ]
--parameter to be used in the function UserLoginID we declare and set the parameter declare @UserLoginID, set @UserLoginID int (data type) , @EmployeeID int
Syntax - Scalar
CREATE FUNCTION fn_Business_Login (@UserLoginID) (@EmplyoeeID)
RETURN INT
AS
BEGIN
CODE SELECT LoginID
FROM dbo.HumanResources.Employe e
WHERE
RETURN INT
END
output that I currently have
LoginID (adventure-works\alan0 ) UserLogintID (alan0) from dbo.HumanResources.Employe
1. Create a UDF that accepts EmployeeID (2012: BusinessEntityID) and returns UserLoginID.
The UserLoginID is the last part of the LoginID column. It’s only the part that comes after the \
select LoginId
,SUBSTRING(LoginID,CHARIND
from [HumanResources].[Employee
--parameter to be used in the function UserLoginID we declare and set the parameter declare @UserLoginID, set @UserLoginID int (data type) , @EmployeeID int
Syntax - Scalar
CREATE FUNCTION fn_Business_Login (@UserLoginID) (@EmplyoeeID)
RETURN INT
AS
BEGIN
CODE SELECT LoginID
FROM dbo.HumanResources.Employe
WHERE
RETURN INT
END
Hard to tell exactly what you're passing in and/or getting back, but this is the general structure you need:
SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
GO
CREATE FUNCTION dbo.fn_Business_Login (
@BusinessEntityID int
)
RETURNS INT
AS
BEGIN
RETURN (
SELECT UserLoginID
FROM dbo.HumanResources.Employe e
WHERE BusinessEntityID = @BusinessEntityID
)
/*end of func*/
END
GO
SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
GO
CREATE FUNCTION dbo.fn_Business_Login (
@BusinessEntityID int
)
RETURNS INT
AS
BEGIN
RETURN (
SELECT UserLoginID
FROM dbo.HumanResources.Employe
WHERE BusinessEntityID = @BusinessEntityID
)
/*end of func*/
END
GO
ASKER
Thank you Scott I was passing on a Business Id to return UserID string from the LoginID
I suspect dbo.HumanResources.Employe e table has a field called LoginID, which contains domain\username but the function is to strip away the domain and just return username:
CREATE FUNCTION dbo.fn_Business_Login (
@BusinessEntityID int
)
RETURNS INT
AS
BEGIN
RETURN
(
SELECT SUBSTRING(LoginID,CHARINDE X('\', LoginID,0) + 1, len(LoginId))
FROM dbo.HumanResources.Employe e
WHERE BusinessEntityID = @BusinessEntityID
)
END
CREATE FUNCTION dbo.fn_Business_Login (
@BusinessEntityID int
)
RETURNS INT
AS
BEGIN
RETURN
(
SELECT SUBSTRING(LoginID,CHARINDE
FROM dbo.HumanResources.Employe
WHERE BusinessEntityID = @BusinessEntityID
)
END
I'm a bit confused.
Shouldn't that mean the return value is varchar and not int?
An int value would strongly imply it's an id of some type that is being returned.
Shouldn't that mean the return value is varchar and not int?
An int value would strongly imply it's an id of some type that is being returned.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
yes this worked and thanks. My biggest Problem is how to make every thing simple. I tend to complicate my thinking and end up lost.
ASKER