Implementing Functions Advanced Database Dr. AlaaEddin Almabhouh
Implementing Functions Introducing Functions Working with Functions Controlling Execution Context
Function A function is a piece of code or routine that accepts parameters and stored as an object in SQL Server. The function always returns a result or result set from invocation. A function can be called within a SELECT statement or even a WHERE clause, whereas a stored procedure must be called using an EXEC[UTE] procedure statement.
Types of Functions Scalar Functions Inline Table-Valued Functions Multi-Statement Table-Valued Functions Built-in Functions
Built-in Functions You need to become familiar with a large number of functions provided to you by Microsoft. Aggregate functions perform operations that combine multiple values into one value by grouping, summarizing, or averaging the values.
Built-in Functions Most commonly used functions String Functions (SUBSTRING, REPLACE, UPPER, etc.) Mathematical Functions (SQRT, ABS, POWER, etc.) Date and Time Functions (GETDATE, DATEDIFF, YEAR, DATENAME, etc.)
What Is a Scalar Function? Scalar Functions: Return a single data value Can be either inline or multi-statement Can return any data type except for text, ntext, image, cursor, and timestamps CREATE FUNCTION [ schema_name. ] function_name ( [ [ AS ][ type_schema_name. ] parameter_data_type [ = default ] [ READONLY ] } [,...n ] ] ) RETURNS return_data_type CREATE FUNCTION [ schema_name. ] function_name ( [ [ AS ][ type_schema_name. ] parameter_data_type [ = default ] [ READONLY ] } [,...n ] ] ) RETURNS return_data_type
Example 1 CREATE FUNCTION int) RETURNS int AS -- Returns the stock level for the product. BEGIN int; = SUM(p.Quantity) FROM Production.ProductInventory p WHERE p.ProductID AND p.LocationID = '6'; END
Example 2 CREATE FUNCTION dbo.ISOweek datetime) RETURNS int WITH EXECUTE AS CALLER AS BEGIN int; as CHAR(4))+'0104'); --Special cases: Jan 1-3 may belong to the previous year IF SET AS CHAR(4))+'12'+ AS CHAR(2)))+1; --Special case: Dec may belong to the next year IF AND 28)) END; returns the week number within a year
Example 3 CREATE FUNCTION GrantTotal INT) RETURNS MONEY AS BEGIN AS MONEY ; = SUM(UnitPrice * Quantity) FROM dbo.[Order Details] WHERE ; END;
What Is a Multi-Statement Table-Valued Function? Multi-statement Table-Valued Function: Returns a TABLE data-type Has a function body defined by BEGIN and END blocks Defines a table-type variable and schema Inserts rows from multiple Transact-SQL statements into the returned table CREATE FUNCTION [ schema_name. ] function_name ( [ [ AS ] [ type_schema_name. ] parameter_data_type [ = default ] [READONLY] } [,...n ] ] ) TABLE CREATE FUNCTION [ schema_name. ] function_name ( [ [ AS ] [ type_schema_name. ] parameter_data_type [ = default ] [READONLY] } [,...n ] ] ) TABLE
Example CREATE FUNCTION dbo.ufn_FindReports INTEGER) TABLE ( EmployeeID int primary key NOT NULL, Name nvarchar(255) NOT NULL, Title nvarchar(50) NOT NULL, EmployeeLevel int NOT NULL, Sort nvarchar (255) NOT NULL
Web Resources server-functions-the-basics/ server-functions-the-basics/
Slide 81 (of 82) Q & A