How to Create Function in SQL Server

Objective: how to create function and then call in SQL Query in SQL Server 2008

Create-Function-in-SQL-Server1.jpg

Click rc->SVF->New Scaler value Function

Create-Function-in-SQL-Server2.jpg

 

CREATE FUNCTION AddTwoNumber

(
      -- Add the parameters for the function here

      @A INT,@B INT

)

RETURNS INT

AS
BEGIN

      -- Declare the return variable here

      DECLARE @Result As INT
 
      -- Add the T-SQL statements to compute the return value here

      SELECT @Result = @A+@B
 

      -- Return the result of the function

      RETURN @Result
END

GO

Now see how I call this function in SQL QUERY

SELECT dbo.AddTwoNumber(2,2)

Create-Function-in-SQL-Server3.jpg