1. Lab Session 1
Learning Objective:
At the end of this lesson, students will be able to:
• Declare variables
• Understand the usage of If...Else statements
• Understand the usage of WHILE Loop With CONTINUE and BREAK Keywords
• Understand the usage of CASE...WHEN expressions
• Use control structures to solve problems
2. Lab Session 1
Learning Objective:
At the end of this lesson, students will be able to:
• Declare variables
• Understand the usage of If...Else statements
• Understand the usage of WHILE Loop With CONTINUE and BREAK Keywords
• Understand the usage of CASE...WHEN expressions
• Use control structures to solve problems
3. Lab Session 1
Functions in SQL Server /Oracle
There are two categories of functions
1. Built in functions
a. SQL Server: ABS,SQRT, GETDATE(),DATEPART, CONVERT,DATEDIFF, etc....
b. Oracle: ABS, SQRT ,SYSDATE() ,COS,
2. User defined functions
a. Scalar user defined functions
b. Inline table valued functions
c. Multi statements table valued functions
4. Lab Session 1
2.a Creating scalar user defined functions using sql server:
Syntax:
CREATE FUNCTION [schema_name.]function_name (@parameter_name parameter_data_type)
RETURNS
return_data_type
[ WITH <function_option>
AS
BEGIN
function_body
RETURN
scalar_expression
END
5. Lab Session 1
Example:
create a function that calculates the net pay of employees
create database EMP
use EMP
create table employee
(
id int primary key,
fname varchar(20),
sex char,
salary int
)
insert into employee values(1,’Abel’,'M’,10000)
insert into employee values(2,’mariyan','F’,11000)
6. Lab Session 1
go
create function netPay(@sal int)
returns float
as
begin
declare @np float
if @sal<150
set @np=@sal
else
set @np=@sal-0.10*@sal
return @np
end
go
7. Lab Session 1
To call the fuction, we have to prefix the funcation_name with the default schema dbo.
Example of calling the function:
select dbo.netPay(employee.salary) from employee
9. Lab Session 1
Example
Create function emp (@id int)
returns table
as
return select * from employee
Where id =@id
To call the function, run the following query:
Select * from dbo.emp(1)
10. Lab Session 1
3.Multi statements table valued functions
CREATE FUNCTION [ schema_name.] function_name ([ { @parameter_name
[ AS ] [
type_schema_name.]parameter_data_type[ = default ] [READONLY] }
[ ,...n]
]
)
RETURNS @return_variable
TABLE <table_type_definition>
[ WITH <function_option> [ , ...n ] ]
[ AS ]
BEGIN
function_body
RETURN
END
[ ; ]
11. Lab Session 1
Example
go
create function empnames(@id int)
returns @report table(fname varchar(50))
as
begin
insert@report select fname from employee where id =@id
return
end
To call the funciton, run the following query
Select * from dbo.empnames(1)
12. Lab Session 1
Exercise
1. Create a function which gives you the maximum of two integers.
2. Suppose that we have two tables named as:
a. Student(fname, studID, sex)
b. Stud_Grade (studID, courseNo,CourseTitle, CrHr, Grade)
Based on the values that could be populated to these tables, create a function that returns
the GPA of a student when you pass studID as an argument to the function
Use the following table structures taken from the Library management of Raya University. Refer
the table structures shown below and write T-SQL statements to answer questions that follow.
Take the following assumptions:
The attribute “BookTitle ” in the book table can have values like C++, Database, Java, etc...
The attribute “City”in the publisher table can have values like Mekelle,Addis Abeba,Maichew, Adigrat, etc...
14. Lab Session 1
Exercise
1) Write a scalar function that returns the average price of Books published by male authors.
2) Write a scalar function that returns the total number of female Authors who published database books
by Mekelle publishers.
3) Write a scalar function that takes ‘Publisher_Name’ as an argument and return total number of authors
who published books in the past ten years.
4) Create an inline table valued function that generate the list of all of who published java books
5) Create a multi statement table valued function to find ID, name and sex of all authors who already
published books. Call this function under a new scalar function that returns the total number of books
published by female authors.