T-SQL Function

  • Function is a database object in SQL Server. Basically, it is a set of SQL statements that accept only input parameters, perform actions and return the result. Function can return an only single value or a table
  • In T-SQL, a function is considered an object
  • Functions compile every time
  • Functions must return a value or result
  • Functions only work with input parameters
  • Try and catch statements are not used in functions
  • From
    • Supports only SELECT or read data. We cannot use function to Insert/Update/Delete records in the database table
    • We can use variables and cursors
    • It accepts only input parameters, do not have output parameters
    • We can call User Defined Functions as part of Select/Insert/Update/Delete
    • It can be nested up to 32 level
    • UDF can have up to 1023 input parameters
    • It can’t return XML data type
    • It doesn’t support Exception handling
  • A function is a set of SQL statements that perform a specific task. Functions foster code reusability. If you have to repeatedly write large SQL scripts to perform the same task, you can create a function that performs that task. Next time instead of rewriting the SQL, you can simply call that function
  • Read more:

Types of functions

Scalar valued function

  • Scalar function takes one or more parameters and returns a single value
  • any function that returns a single piece of data is called a Scalar-valued Function
  • Scalar- valued Function can be used as part of the SELECT statement, part of the where clause and or part of the order by clause.

Creating function in SSMS


Creating function in ssms Step 1


Creating function in ssms Step 2


Creating function in ssms Step 3


Creating function in ssms Step 4

Creating function using T-SQL

CREATE FUNCTION getEmail(@PersonName AS varchar(250))
RETURNS varchar(250)
	DECLARE @emailAddress AS varchar(250)
	SELECT @emailAddress = EmailAddress
	FROM [Application].[People]
	WHERE [FullName] = @PersonName

	RETURN @emailAddress


Example - a custom function to convert degrees celsius into degrees fahrenheit

-- create a custom function to convert degrees celsius into degrees fahrenheit
CREATE FUNCTION Warehouse.ToFahrenheit (@Celsius decimal(10,2))
RETURNS decimal(10,2)
	DECLARE @Fahrenheit decimal(10,2);
	SET @Fahrenheit = (@Celsius * 1.8 + 32);
	RETURN @Fahrenheit

Table valued function

  • Table-valued Function. As the name implies, the results of this function will be an entire table
  • a view would do some similar things. But, remember you can’t pass a parameter to a view. So the strength of Tabled-valued Functions is that we can pass one or more parameters to it and get different results based on those parameters. Other than that they function largely like tables or views

Creating a table valued function

CREATE FUNCTION dbo.AuthorsByStatus(@isActive int)
RETURNS @AuthorsTable TABLE 
(	[FirstName] [varchar](50) NOT NULL,
	[LastName] [varchar](50) NOT NULL,
	[PhoneNumber] [varchar](50) NULL,
	[Address] [varchar](50) NULL,
	[City] [varchar](50) NULL,
	[State] [varchar](50) NULL,
	[Zip] [varchar](50) NULL,
	[Active] [int] NULL

	INSERT INTO @AuthorsTable
	    SELECT * 
	    WHERE Active = @isActive



SELECT * FROM [dbo].[AuthorsByStatus](1)