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

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^