Hello everyone,
I am going to share the code sample for create sql functions.
The example for the same as given below.
Table of Contents
- Add returns type like varchar, int etc.
- Declare the return variable.
- Add the T-sql statements to compute the return value.
- 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