Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 8

Functions

Lesson 3: Creating Functions

Types of Functions
What Is a Scalar Function?
What Is an Inline Table-Valued Function?
What Is a Multi-Statement Table-Valued Function?
Practice: Creating Functions
Types of Functions

Scalar functions
 Similar to a built-in function
 Return a single value
Inline table-valued functions
 Similar to a view with parameters
 Return a table as the result of single SELECT statement
Multi-statement table-valued functions
 Similar to a stored procedure
 Return a new table as the result of multi statements
What Is a Scalar Function?

RETURNS clause specifies data type


Function is defined within a BEGIN…END block
create function sumsold (@productid int) returns int
as
begin
declare @result int
select @result=sum(quantity)
from [order details]
where productid=@productid
return @result
end

Can be invoked anywhere a scalar expression of the same data


type is allowed
select productid,productname,dbo.sumsold(productid)
from products
What Is an Inline Table-Valued Function?

RETURNS specifies table as data type


Format is defined by result set
Content of function is a SELECT statement
create function fn_CustomersNamesInRegion (@region
varchar(30))
returns table
as
return
(
select customerid,companyname
from customers

where region=@region)

select * from fn_CustomersNamesInRegion ('wa')


What Is a Multi-Statement Table-Valued Function?
RETURNS specifies table data type and defines structure
BEGIN and END enclose multiple statements
create function fn_Employees(@length varchar(9))returns @tbl_Employees table
(EmployeeID int primary key,EmployeeName varchar (50))
as
begin
if (@length='shortname')
insert @tbl_Employees select EmployeeId ,lastname from employees
else
if (@length='longname')
insert @tbl_Employees select EmployeeId,lastname+ ''+firstname from employee
return
end

select * from fn_Employees ('longname')


select * from fn_Employees ('shortname')
Lab

■Create a scalar user-defined function named GetMaximumDiscountForCategory


within the Sales schema that retrieves the maximum discount percentage
currently available for a specific category. Create an @Category varchar(50)
parameter to limit the results based on the category, and use the GETDATE
function to limit the rows based on whether the discount is currently available
(using Sales.SpecialOffer).

■ Create an inline table-valued user-defined function named GetDiscountsForDate


within the Sales schema that retrieves the discounts. The function accepts an
@DateToCheck datetime parameter to filter the discounts based on the provided
date. This allows Adventure Works to test what discounts will be available on a
specific date.
■ Create a multi-statement table-valued user-defined function
named GetDiscountedProducts within the Sales schema that
uses a complex query to retrieve products that have a
discount. This complex query will be provided to you. The
function accepts an @IncludeHistory bit parameter to filter
the returned table based on whether the discount history
information is required or only the current information is
needed. The returned table will include the following
definition.(using Sales.SpecialOfferProduct, Sales.SpecialOffer, Production.Product)
ProductID int,Name nvarchar(50)
ListPrice money,DiscountDescription nvarchar(255)
DiscountPercentage smallmoney,DiscountAmount money
DiscountedPrice money

You might also like