954,116 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How can I create a user defined function

Hello,

Can anyone explain to me how I can create a function that I can use in SQL.

Thanks

Dark_Omen
Posting Pro
573 posts since Apr 2004
Reputation Points: 23
Solved Threads: 6
 

It's pretty easy.

CREATE FUNCTION MyFunction(@variableOne datatype)
RETURNS datatype
AS
BEGIN

-- put function logic here

RETURN @variable

END

Then to call the function, you would use it in any SQL statement. For example

SELECT FIELD1, MyFunction(FIELD2) as ALIAS
FROM SOMETABLE;

Hope this helps

Andy

nicentral
Junior Poster
166 posts since Apr 2005
Reputation Points: 14
Solved Threads: 5
 

Thanks, that was very helpful

Dark_Omen
Posting Pro
573 posts since Apr 2004
Reputation Points: 23
Solved Threads: 6
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You