Functions
Functions
Contents
Functions ................................................................................................................................................. 1
Creating functions ................................................................................................................................... 1
Examples ................................................................................................................................................. 1
References .............................................................................................................................................. 3
Functions
Creates a user-defined function in SQL Server and Azure SQL Database. A user-defined function is a
Transact-SQL or common language runtime (CLR) routine that accepts parameters, performs an
action, such as a complex calculation, and returns the result of that action as a value. The return
value can either be a scalar (single) value or a table.
Creating functions
Examples
The following example creates the user-defined function ISOweek. This function takes a date
argument and calculates the ISO week number. For this function to calculate correctly, SET DATEFIRST
1 must be invoked before the function is called.
The example also shows using the EXECUTE AS clause to specify the security context in which a stored
procedure can be executed. In the example, the option CALLER specifies that the procedure will be
executed in the context of the user that calls it. The other options that you can specify are SELF,
OWNER, and user_name.
1|Page
SET @ISOweek= DATEPART(wk,@DATE)+1
-DATEPART(wk,CAST(DATEPART(yy,@DATE) as CHAR(4))+'0104');
--Special cases: Jan 1-3 may belong to the previous year
IF (@ISOweek=0)
SET @ISOweek=dbo.ISOweek(CAST(DATEPART(yy,@DATE)-1
AS CHAR(4))+'12'+ CAST(24+DATEPART(DAY,@DATE) AS CHAR(2)))+1;
--Special case: Dec 29-31 may belong to the next year
IF ((DATEPART(mm,@DATE)=12) AND
((DATEPART(dd,@DATE)-DATEPART(dw,@DATE))>= 28))
SET @ISOweek=1;
RETURN(@ISOweek);
END;
GO
SET DATEFIRST 1;
SELECT dbo.ISOweek(CONVERT(DATETIME,'12/26/2004',101)) AS 'ISO Week';
The following example returns an inline table-valued function in the AdventureWorks2012 database.
It returns three columns ProductID, Name and the aggregate of year-to-date totals by store as YTD
Total for each product sold to the store.
2|Page
RETURN @return
END
The following SQL statements will generate the result set shown in the accompanying figure.
print dbo.WhichContinent('USA')
print dbo.WhichContinent('Brazil')
print dbo.WhichContinent('South Africa')
print dbo.WhichContinent('France')
The following statement creates a table-valued function which in turn invokes the scalar-valued
function.
CREATE FUNCTION CustomersByContinent
(@Continent varchar(30))
RETURNS TABLE
AS
RETURN
SELECT dbo.WhichContinent(Customers.Country) as continent,
customers.*
FROM customers
WHERE dbo.WhichContinent(Customers.Country) = @Continent
References
1. MSDN Library
3|Page