MS SQL Server Concepts and Programming Question:

How To Create a Simple User Defined Function in MS SQL Server?

MS SQL Server Interview Question
MS SQL Server Interview Question

Answer:

If you want to create a simple user defined function, you can use the "CREATE FUNCTION" command with a statement block in a simple format as shown in below:

CREATE FUNCTION function_name()
RETURNS data_type
AS BEGIN
statement_1;
statement_2;
...
statement_n;
RETURN expression;
END;
GO

The following tutorial exercise shows you how to create a simple user defined function:

USE GlobalGuideLineDatabase;
GO

CREATE FUNCTION Welcome()
RETURNS VARCHAR(40)
AS BEGIN
RETURN 'Welcome to globalguideline.com';
END;
GO

PRINT dbo.Welcome();
GO
Welcome to globalguideline.com



Previous QuestionNext Question
What Are the Differences between User Defined Functions and Stored Procedures?How To Use User Defined Functions in Expressions?