Presentation is loading. Please wait.

Presentation is loading. Please wait.

Implementing Functions Advanced Database Dr. AlaaEddin Almabhouh.

Similar presentations


Presentation on theme: "Implementing Functions Advanced Database Dr. AlaaEddin Almabhouh."— Presentation transcript:

1 Implementing Functions Advanced Database Dr. AlaaEddin Almabhouh

2 Implementing Functions  Introducing Functions  Working with Functions  Controlling Execution Context

3 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.

4 Types of Functions Scalar Functions Inline Table-Valued Functions Multi-Statement Table-Valued Functions Built-in Functions

5 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.

6 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.) http://msdn.microsoft.com/en-us/library/ms174318.aspx

7 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 ( [ { @parameter_name [ AS ][ type_schema_name. ] parameter_data_type [ = default ] [ READONLY ] } [,...n ] ] ) RETURNS return_data_type CREATE FUNCTION [ schema_name. ] function_name ( [ { @parameter_name [ AS ][ type_schema_name. ] parameter_data_type [ = default ] [ READONLY ] } [,...n ] ] ) RETURNS return_data_type

8 Example 1 CREATE FUNCTION dbo.ufnGetInventoryStock(@ProductID int) RETURNS int AS -- Returns the stock level for the product. BEGIN DECLARE @ret int; SELECT @ret = SUM(p.Quantity) FROM Production.ProductInventory p WHERE p.ProductID = @ProductID AND p.LocationID = '6'; return @ret END

9 Example 2 CREATE FUNCTION dbo.ISOweek (@DATE datetime) RETURNS int WITH EXECUTE AS CALLER AS BEGIN DECLARE @ISOweek int; 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; returns the week number within a year

10 Example 3 CREATE FUNCTION GrantTotal (@OrderID INT) RETURNS MONEY AS BEGIN DECLARE @OrderTotal AS MONEY ; SELECT @OrderTotal = SUM(UnitPrice * Quantity) FROM dbo.[Order Details] WHERE OrderID=@OrderID RETURN @OrderTotal ; END;

11 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 ( [ { @parameter_name [ AS ] [ type_schema_name. ] parameter_data_type [ = default ] [READONLY] } [,...n ] ] ) RETURNS @return_variable TABLE CREATE FUNCTION [ schema_name. ] function_name ( [ { @parameter_name [ AS ] [ type_schema_name. ] parameter_data_type [ = default ] [READONLY] } [,...n ] ] ) RETURNS @return_variable TABLE

12 Example CREATE FUNCTION dbo.ufn_FindReports (@InEmpID INTEGER) RETURNS @retFindReports 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

13 Web Resources  http://technet.microsoft.com/en-us/library/ms191320.aspx http://technet.microsoft.com/en-us/library/ms191320.aspx  http://msdn.microsoft.com/en-us/library/ms186755.aspx http://msdn.microsoft.com/en-us/library/ms186755.aspx  https://www.simple-talk.com/sql/t-sql-programming/sql- server-functions-the-basics/ https://www.simple-talk.com/sql/t-sql-programming/sql- server-functions-the-basics/

14 Slide 81 (of 82) Q & A


Download ppt "Implementing Functions Advanced Database Dr. AlaaEddin Almabhouh."

Similar presentations


Ads by Google