Docsity

Database Programmability Functions, Stored Procedures, Triggers and Transactions, Lecture notes of Database Programming

Partial preview of the text

Download Database Programmability Functions, Stored Procedures, Triggers and Transactions and more Lecture notes Database Programming in PDF only on Docsity!

Database Programmability

Functions, Stored Procedures, Triggers and Transactions

  • Functions
  • Stored Procedures
  • Transactions
  • ACID Model
  • Triggers
  • Database Security Table of Contents
  • At its core, a function receives input and produces output Functions: Basic Definition
  • Scalar functions (like SQRT(…) ).
    • Similar to the built-in functions
    • Return a single value
  • Table-valued functions.
    • Similar to a view with parameters
    • Return a table as a result of single SELECT statement
  • Aggregate functions (like SUM(…) )
    • Perform calculation over set of inputs values.
    • Defined through external .NET functions.

Types of User-Defined Functions

  • Functions are called using schemaName.functionName Execute Functions SELECT [ProjectID], [StartDate], [EndDate], dbo.udf_ProjectDurationWeeks([StartDate],[EndDate]) AS ProjectWeeks FROM [SoftUni].[dbo].[Projects] ProjectID StartDate EndDate ProjectWeeks 1 2016 - 09 - 01 2016 - 10 - 07 5 2 2016 - 10 - 01 2016 - 10 - 07 1 3 2015 - 10 - 07 NULL 52 Call the Function
  • Write a function ufn_GetSalaryLevel(@salary MONEY)

that receives salary of an employee and returns the level

of the salary.

  • If salary is < 30000 return “Low”
  • If salary is between 30000 and 50000 (inclusive) return “Average”
  • If salary is > 50000 return “High” Problem: Salary Level Function

Solution: Salary Level Function (2) DECLARE @salaryLevel VARCHAR(10) IF (@salary < 30000) SET @salaryLevel = 'Low' ELSE IF(@salary >= 30000 AND @salary <= 50000) SET @salaryLevel = 'Average' ELSE SET @salaryLevel = 'High' RETURN @salaryLevel Variable IF Statement Return Result

STORED PROCEDURES

  • Syntax: CREATE PROCEDURE … AS …
  • Example: Creating Stored Procedures USE SoftSchool GO CREATE PROC dbo.usp_SelectEmployeesBySeniority AS SELECT * FROM Employees WHERE DATEDIFF(Year, HireDate, GETDATE()) > 5 GO Procedure Name Procedure Logic
  • Executing a stored procedure by EXEC.
  • Executing a stored procedure within an INSERT

statement.

Executing Stored Procedures

EXEC usp_SelectEmployeesBySeniority

INSERT INTO Customers

EXEC usp_SelectEmployeesBySeniority

  • DROP PROCEDURE
  • You could check if any objects depend on the stored

procedure by executing the system stored procedure

sp_depends

Dropping Stored Procedures

DROP PROC usp_SelectEmployeesBySeniority

EXEC sp_depends 'usp_SelectEmployeesBySeniority'

STORED PROCEDURES

Using Parameters

Parameterized Stored Procedures – Example CREATE PROC usp_SelectEmployeesBySeniority(@minYearsAtWork int = 5) AS SELECT FirstName, LastName, HireDate, DATEDIFF(Year, HireDate, GETDATE()) as Years FROM Employees WHERE DATEDIFF(Year, HireDate, GETDATE()) > @minYearsAtWork ORDER BY HireDate GO EXEC usp_SelectEmployeesBySeniority 10 EXEC usp_SelectEmployeesBySeniority Procedure Name Procedure Logic Usage

  • Passing values by parameter name
  • Passing values by position Passing Parameter Values EXEC usp_AddCustomer @customerID = 'ALFKI', @companyName = 'Alfreds Futterkiste', @address = 'Obere Str. 57', @city = 'Berlin', @phone = '030-0074321' EXEC usp_AddCustomer 'ALFKI2', 'Alfreds Futterkiste', 'Obere Str. 57', 'Berlin', '030-0074321'