sql server create functions example

SQL Server create functions example

Hello everyone,  I am going to share the code sample for create sql functions

Table of Contents
  1. Add returns type like varchar, int etc.
  2. Declare the return variable.
  3. Add the T-sql statements to compute the return value.
  4. Return the result of the function.

The example for the same as given below.

CREATE FUNCTION [dbo].[fun_GetOrderType]
(
            @Return SMALLINT,
            @Reprocess SMALLINT,
            @Cancel SMALLINT
)
RETURNS VARCHAR(10)
AS
BEGIN
            -- DECLARE THE RETURN VARIABLE HERE.
            DECLARE @type VARCHAR(10)='XYZ'

            -- ADD THE T-SQL STATEMENTS TO COMPUTE THE RETURN VALUE HERE.
            IF (@Return = 1)
                        SET @type = 'RE'
            ELSE IF (@Reprocess = 2)
                        SET @type = 'RPC'
            ELSE IF (@Cancel = 3)
                        SET @type = 'CNL'

            -- RETURN THE RESULT OF THE FUNCTION
            RETURN @type
END



ANIL SINGH

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Google Amazon and Flipkart Book Store!

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.
ASK Questions
SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book