Presentation is loading. Please wait.

Presentation is loading. Please wait.

Create Stored Procedures and Functions 98-364 Database Management Fundamentals LESSON 2.4.

Similar presentations


Presentation on theme: "Create Stored Procedures and Functions 98-364 Database Management Fundamentals LESSON 2.4."— Presentation transcript:

1 Create Stored Procedures and Functions 98-364 Database Management Fundamentals LESSON 2.4

2 98-364 Database Management Fundamentals LESSON 2.4 Lesson Overview In this lesson, you will learn about:  Functions  Aggregate Functions  Scalar Functions  User Defined Functions  Stored Procedures  Benefits of Stored Procedures

3 98-364 Database Management Fundamentals LESSON 2.4 What is a function?  The purpose of, or the action carried out by a program or routine  A function is a routine that returns a value  routine - Any section of code that can be invoked (executed) within a program. A routine usually has a name (identifier) associated with it and is executed by referencing that name. Related terms include function, procedure, and subroutine  Functions and routines can be synonymous and they are commands that return a value

4 98-364 Database Management Fundamentals LESSON 2.4 Aggregate Functions Return a single value, calculated from values in a column Examples:  AVG() - Returns the average value SELECT AVG(column_name) FROM table_name  COUNT() - Returns the number of rows SELECT COUNT(column_name) FROM table_name  FIRST() - Returns the first value SELECT FIRST(column_name) FROM table_name

5 98-364 Database Management Fundamentals LESSON 2.4 Aggregate Functions (Continued)  LAST() - Returns the last value SELECT LAST(column_name) FROM table_name  MAX() - Returns the largest value SELECT MAX(column_name) FROM table_name  MIN() - Returns the smallest value SELECT MIN(column_name) FROM table_name  SUM() - Returns the sum SELECT SUM(column_name) FROM table_name

6 98-364 Database Management Fundamentals LESSON 2.4 Scalar Functions Scalar Functions return a single value, based on the input value. Examples:  UCASE() - Converts text in a field to upper case SELECT UCASE(column_name) FROM table_name  LCASE() - Converts a field to lower case SELECT LCASE(column_name) FROM table_name  MID() - Selects characters from a text field SELECT MID(column_name,start[,length]) FROM table_name

7 98-364 Database Management Fundamentals LESSON 2.4 Scalar Functions (Continued)  LEN() - Returns the length of a text field SELECT LEN(column_name) FROM table_name  ROUND() - Rounds a numeric field to the number of decimals specified SELECT ROUND(column_name,decimals) FROM table_name

8 98-364 Database Management Fundamentals LESSON 2.4 User Defined Functions  Written incompact SQL code which can accept parameters and returns either a value or a table  Advantage - can be used in Select, Where, or Case statements. They also can be used to create joins  Cannot be used to modify base table information

9 98-364 Database Management Fundamentals LESSON 2.4 User Defined Function (Continued)  To create Function CREATE FUNCTION [Function name] ([input variables]) RETURNS varchar(10) AS BEGIN DECLARE [output variable] varchar(10) RETURN [output variable] END

10 98-364 Database Management Fundamentals LESSON 2.4 User Defined Function (Continued)  To call a function: SELECT [Function name] ([input variables]) To remove a function: DROP FUNCTION [Function name]

11 98-364 Database Management Fundamentals LESSON 2.4 Stored Procedures  Precompiled group of SQL statements written by users and saved to the database  A SQL statement(s) generated by the user that is saved in the database Create a stored procedure CREATE PROCEDURE [sp_Procedure Name] AS SELECT * FROM [Table] sp = stored procedure (This is a standard naming convention)

12 98-364 Database Management Fundamentals LESSON 2.4 Stored Procedures (Continued) Create a stored procedure for multi-site campus CREATE PROCEDURE sp_getclass @campuslocation varchar(30) AS SELECT ClassName, ClassSection FROM Current_term WHERE campus = @campuslocation

13 98-364 Database Management Fundamentals LESSON 2.4 Stored Procedures (Continued) Call a stored procedure EXEC [procedure name] ([input variables]) EXECUTE sp_getclass ‘Twin Cities‘  This stored procedure will return a two item list or view ( ClassName,ClassSection) of all classes on the “Twin Cities” campus

14 98-364 Database Management Fundamentals LESSON 2.4 Benefits of Stored Procedures  Speed - Stored procedures are pre-compiled  Code reuse - Stored procedures often involve complex code which only has to written once  Security - Permissions can be granted for stored procedures while being restricted for the underlying tables  Reduced traffic between client and server - The query is stored on the server and only the procedure call gets sent, so traffic to the server is decreased

15 98-364 Database Management Fundamentals LESSON 2.4 Lesson Review Quiz for Discussion  What is the basic difference between a function and a stored procedure?  What would be the Stored Procedure call for a list of classes on the Duluth campus? (slide 13)  What can we add to our sp_getclass stored procedure to make it more useful? (slide 13)


Download ppt "Create Stored Procedures and Functions 98-364 Database Management Fundamentals LESSON 2.4."

Similar presentations


Ads by Google