STORED
PROCEDURESBY
P R O F M U K A S A N O O R D I N
M S K AT H E R I N E K E M I R E M B E
M S A K AT U S I N G U Z A A N N A H
E N J O Y T H E S H O W
DEFN
• A stored procedure is a set of SQL statements
that are stored in the server and executed as a
block.
WHY STORED
PROCEDURES
 STORED PROCEDURES ARE FAST !
IF YOU HAVE A REPETITIVE TASK THAT REQUIRES
CHECKING, LOOPING, MULTIPLE STATEMENTS , AND
NO USER INTERACTION
 REDUCE REWORK - CLIENT APPLICATIONS IN
DIFFERENT L ANGUAGES NEED TO PERFORM THE
SAME OPERATIONS .
 STORED PROCEDURES ARE COMPONENTS!
SUPPOSE THAT YOU CHANGE YOUR HOST
L ANGUAGE NO PROBLEM, THE LOGIC IS IN THE
DATABASE NOT THE APPLICATION
WHY STORED PROCEDURES
CONTD..
• Stored procedures are portable! When you write your stored
procedure in SQL, you know that it will run on every platform
that MySQL runs on, without obliging you to install an
additional run time environment package, or set permissions for
program execution in the operating system, or deploy different
packages if you have different computer types.
WHY STORED PROCEDURES
CONTD..
• Stored procedures are stored! If you write a procedure with the
right naming conventions, for example saying
chequing_withdrawal for a bank transaction, then people who
want to know about chequing can find your procedure
• Stored procedures are migratory! MySQL adheres fairly closely
to the SQL:2003 standard.
• Improved Security - Applications and users may have no access
to the database tables directly, but can execute only specific
stored routines.
• Improved Performance – Less data and fewer commands sent
between the client and server.
LEGAL IDENTIFIERS
• Procedure Names are not case sensitive, so 'p1' and 'P1' are the
same name.
• You cannot use two procedures with the same name in the
same database
• Some SQL commands are not allowed: LOAD DATA, (UN)LOCK
TABLES, CHECK,REPAIR
• Stored Procedures have all limitations of Stored Functions if
called from withing a stored function UNDO handlers are not
supported.
• FOR loops are not supported. Identifiers cannot be passed in
variables
LEGAL IDENTIFIERS CONTD…
• There are no stored routine debugging facilities.
WHAT SQL STATEMENTS ARE
LEGAL IN A PROCEDURE BODY
• You can make a procedure that contains pretty well anything,
including INSERT, UPDATE, DELETE,SELECT, DROP, CREATE,
REPLACE, and so on
SYNTAX FOR CREATING A
PROCEDURE
• CREATE
• PROCEDURE sp_name ([proc_parameter[,...]])
• [characteristic ...] routine_body
EXPLANATIONS
• proc_parameter:
[mode] param_name datatype
A mode can be an IN meaning input OUT for output and INOUT for
both
• The parameter list is available with in the parentheses. Parameter can
be declared to use any valid data type By default each parameter is
an IN parameter
• The stored procedure is created on an active database
• An OUT parameter is used to pass the value from the procedure to
the caller but its visible to the caller.
• An INOUT parameter is initialized by the caller and it can be
modified by the procedure, and any change made by the procedure
is visible to the caller.
• Any valid MySQL data type
EXPLANATION CONTD…
• routine_body:
Valid SQL procedure statement
Such as
CREATE
PROCEDURE PROCEDURE_Name ( IN||OUT||INOUT par1
datatype1,[……]
Routine_body
EXAMPLE 1
Similar
example
CALL THE PROCEDURE
• Syntax for calling a procedure
CALL procedure_name(<argument(s)>)
Argument is put if a parameter exists in a procedure if not placed
its considered as null for example
BEGIN... END SYNTAX
• [begin_label:] BEGIN
[block of statement(s)]
• END [end_label]
SYNTAX EXPLAINED
• BEGIN ... END syntax is used for writing compound statements,
which can appear within stored routines and triggers.
• Each statement within statement_list must be terminated by a
semicolon (;) statement delimiter.
EXAMPLE OF PROCEDURE WITH
BEGIN…..END BLOCK• Well the first example does nothing
Second example does something
IN EXAMPLE
When this is called the value of @entered will be
that name from an input
OUT EXAMPLE
This will output what is in numb
LOCAL VARIABLES• The DECLARE statement is used to define local variables in a
BEGIN..END statement.
• DECLARE var_name type
• ex. DECLARE name VARCHAR(20);
• ex2. DECLARE v1 INT UNSIGNED DEFAULT 10;
• Variable SET Statement
• SET a = expression [, b = expression ]...
• ex. SET name = 'bob';
• ex2. SET v1 = v1+5;
DECLARE HANDLERS
• The DECLARE... CONDITION FOR statement
• specifies conditions that need specific handling. It
• associates a name with a specified error condition.
• The DECLARE ... HANDLER statement specifies
• handlers that each may deal with one or more
• conditions. If one of these conditions occurs, the
• specified statement is executed. statement can be
• a simple statement (for example, SET var_name =
• value), or it can be a compound
COMPLEX EXAMPLE
DROPPING STORED
PROCEDURE
• After they are created, stored procedures remain on
the server, ready for use, until dropped.
• The drop command removes the stored procedure
from the server.
• DROP PROCEDURE procedure_name;
• Example
IF…..THEN ELSEIF AND ELSE
• Syntax
• IF search_condition THEN statement_list
• [ELSEIF search_condition THEN statement_list] ...
• [ELSE statement_list]
• END IF
• CASE:
EXAMPLE
CASE…….WHEN USE
• Syntax
CASE case_value
[WHEN when_value THEN statement_list]...
[ELSE statement_list]
END CASE
Or:
CASE
[WHEN search_condition THEN statement_list]……
[ELSE statement_list]
END CASE
EXAMPLE
LOOPS
• These include while,loop,and repetitive
• Using Loop
• syntax
• [begin_label:] LOOP
• statement_list
• END LOOP [end_label]
• LEAVE:
• LEAVE label
USING REPEAT
• Syntax
[begin_label:] REPEAT
statement_list
UNTIL search_condition
END REPEAT [end_label]
EXAMPLE
USING WHILE
• WHILE Syntax
• [begin_label:] WHILE search_condition DO
• statement_list
• END WHILE [end_label]
EXAMPLE
ITERATE USE
• Syntax
ITERATE:
ITERATE label
EXAMPLE
WHAT IS A STORED FUNCTION
• What is a Stored Function
• A stored function is a set of SQL statements that
are stored in the server and executed as a block
like a stored routine but with some additional
limitations.
• A stored function may return only one result for
each input value.
• A stored function can be called from within other
SQL commands such as SELECT.
STORED FUNCTION
LIMITATIONS 1/2
• All Stored Procedure limitations plus...
• Also applied with ALTER, CREATE, DROP, LOAD, TRUNCATE,
RENAME
• A function can process a result set if you use a cursor and
FETCH statements.
• Recursive statements,
• Functions cannot call themselves
STORED FUNCTION SYNTAX
• CREATE
FUNCTION sp_name ([func_parameter[,...]])
RETURNS type
routine_body
DESCRIPTION
• func_parameter:
param_name type
• type:
• Any valid MySQL data type
• routine_body:
Valid SQL procedure statement
RETURN STATEMENT• Syntax
• RETURN expr
DESCRIPTION
• The RETURN statement terminates execution of a
• stored function and returns the value expr to the
• function caller.
• NOTE:
1. There must be at least one RETURN statement in a stored function.
2. There may be more than one if the function has multiple exit
points. This statement is not used in stored procedures or triggers.
COMMIT OR ROLLBACK
CONCEPTS
• By default most database systems are configured to
automatically COMMIT an SQL statement, i.e save it in the
database.
• Nevertheless this is possible in making transactions to either
commit data to database or to roll back.
SYNTAX OF A TRANSACTION
START TRANSACTION
[SQL statement(s)]
COMMIT / ROLLBACK
EXAMPLE
OUTPUT
CONTACT
• muksnoor@gmail.com
• Or
• +256751861670
REFERENCES
• See: http://dev.mysql.com/doc/refman/5.0/en/implicit-
commit.htm

Stored procedures

  • 1.
    STORED PROCEDURESBY P R OF M U K A S A N O O R D I N M S K AT H E R I N E K E M I R E M B E M S A K AT U S I N G U Z A A N N A H E N J O Y T H E S H O W
  • 2.
    DEFN • A storedprocedure is a set of SQL statements that are stored in the server and executed as a block.
  • 3.
    WHY STORED PROCEDURES  STOREDPROCEDURES ARE FAST ! IF YOU HAVE A REPETITIVE TASK THAT REQUIRES CHECKING, LOOPING, MULTIPLE STATEMENTS , AND NO USER INTERACTION  REDUCE REWORK - CLIENT APPLICATIONS IN DIFFERENT L ANGUAGES NEED TO PERFORM THE SAME OPERATIONS .  STORED PROCEDURES ARE COMPONENTS! SUPPOSE THAT YOU CHANGE YOUR HOST L ANGUAGE NO PROBLEM, THE LOGIC IS IN THE DATABASE NOT THE APPLICATION
  • 4.
    WHY STORED PROCEDURES CONTD.. •Stored procedures are portable! When you write your stored procedure in SQL, you know that it will run on every platform that MySQL runs on, without obliging you to install an additional run time environment package, or set permissions for program execution in the operating system, or deploy different packages if you have different computer types.
  • 5.
    WHY STORED PROCEDURES CONTD.. •Stored procedures are stored! If you write a procedure with the right naming conventions, for example saying chequing_withdrawal for a bank transaction, then people who want to know about chequing can find your procedure • Stored procedures are migratory! MySQL adheres fairly closely to the SQL:2003 standard. • Improved Security - Applications and users may have no access to the database tables directly, but can execute only specific stored routines. • Improved Performance – Less data and fewer commands sent between the client and server.
  • 6.
    LEGAL IDENTIFIERS • ProcedureNames are not case sensitive, so 'p1' and 'P1' are the same name. • You cannot use two procedures with the same name in the same database • Some SQL commands are not allowed: LOAD DATA, (UN)LOCK TABLES, CHECK,REPAIR • Stored Procedures have all limitations of Stored Functions if called from withing a stored function UNDO handlers are not supported. • FOR loops are not supported. Identifiers cannot be passed in variables
  • 7.
    LEGAL IDENTIFIERS CONTD… •There are no stored routine debugging facilities.
  • 8.
    WHAT SQL STATEMENTSARE LEGAL IN A PROCEDURE BODY • You can make a procedure that contains pretty well anything, including INSERT, UPDATE, DELETE,SELECT, DROP, CREATE, REPLACE, and so on
  • 9.
    SYNTAX FOR CREATINGA PROCEDURE • CREATE • PROCEDURE sp_name ([proc_parameter[,...]]) • [characteristic ...] routine_body
  • 10.
    EXPLANATIONS • proc_parameter: [mode] param_namedatatype A mode can be an IN meaning input OUT for output and INOUT for both • The parameter list is available with in the parentheses. Parameter can be declared to use any valid data type By default each parameter is an IN parameter • The stored procedure is created on an active database • An OUT parameter is used to pass the value from the procedure to the caller but its visible to the caller. • An INOUT parameter is initialized by the caller and it can be modified by the procedure, and any change made by the procedure is visible to the caller. • Any valid MySQL data type
  • 11.
    EXPLANATION CONTD… • routine_body: ValidSQL procedure statement Such as CREATE PROCEDURE PROCEDURE_Name ( IN||OUT||INOUT par1 datatype1,[……] Routine_body
  • 12.
  • 13.
    CALL THE PROCEDURE •Syntax for calling a procedure CALL procedure_name(<argument(s)>) Argument is put if a parameter exists in a procedure if not placed its considered as null for example
  • 14.
    BEGIN... END SYNTAX •[begin_label:] BEGIN [block of statement(s)] • END [end_label]
  • 15.
    SYNTAX EXPLAINED • BEGIN... END syntax is used for writing compound statements, which can appear within stored routines and triggers. • Each statement within statement_list must be terminated by a semicolon (;) statement delimiter.
  • 16.
    EXAMPLE OF PROCEDUREWITH BEGIN…..END BLOCK• Well the first example does nothing Second example does something
  • 17.
    IN EXAMPLE When thisis called the value of @entered will be that name from an input
  • 18.
    OUT EXAMPLE This willoutput what is in numb
  • 19.
    LOCAL VARIABLES• TheDECLARE statement is used to define local variables in a BEGIN..END statement. • DECLARE var_name type • ex. DECLARE name VARCHAR(20); • ex2. DECLARE v1 INT UNSIGNED DEFAULT 10; • Variable SET Statement • SET a = expression [, b = expression ]... • ex. SET name = 'bob'; • ex2. SET v1 = v1+5;
  • 20.
    DECLARE HANDLERS • TheDECLARE... CONDITION FOR statement • specifies conditions that need specific handling. It • associates a name with a specified error condition. • The DECLARE ... HANDLER statement specifies • handlers that each may deal with one or more • conditions. If one of these conditions occurs, the • specified statement is executed. statement can be • a simple statement (for example, SET var_name = • value), or it can be a compound
  • 21.
  • 22.
    DROPPING STORED PROCEDURE • Afterthey are created, stored procedures remain on the server, ready for use, until dropped. • The drop command removes the stored procedure from the server. • DROP PROCEDURE procedure_name; • Example
  • 23.
    IF…..THEN ELSEIF ANDELSE • Syntax • IF search_condition THEN statement_list • [ELSEIF search_condition THEN statement_list] ... • [ELSE statement_list] • END IF • CASE:
  • 24.
  • 25.
    CASE…….WHEN USE • Syntax CASEcase_value [WHEN when_value THEN statement_list]... [ELSE statement_list] END CASE Or: CASE [WHEN search_condition THEN statement_list]…… [ELSE statement_list] END CASE
  • 26.
  • 27.
    LOOPS • These includewhile,loop,and repetitive • Using Loop • syntax • [begin_label:] LOOP • statement_list • END LOOP [end_label] • LEAVE: • LEAVE label
  • 28.
    USING REPEAT • Syntax [begin_label:]REPEAT statement_list UNTIL search_condition END REPEAT [end_label]
  • 29.
  • 30.
    USING WHILE • WHILESyntax • [begin_label:] WHILE search_condition DO • statement_list • END WHILE [end_label]
  • 31.
  • 32.
  • 33.
  • 34.
    WHAT IS ASTORED FUNCTION • What is a Stored Function • A stored function is a set of SQL statements that are stored in the server and executed as a block like a stored routine but with some additional limitations. • A stored function may return only one result for each input value. • A stored function can be called from within other SQL commands such as SELECT.
  • 35.
    STORED FUNCTION LIMITATIONS 1/2 •All Stored Procedure limitations plus... • Also applied with ALTER, CREATE, DROP, LOAD, TRUNCATE, RENAME • A function can process a result set if you use a cursor and FETCH statements. • Recursive statements, • Functions cannot call themselves
  • 36.
    STORED FUNCTION SYNTAX •CREATE FUNCTION sp_name ([func_parameter[,...]]) RETURNS type routine_body
  • 37.
    DESCRIPTION • func_parameter: param_name type •type: • Any valid MySQL data type • routine_body: Valid SQL procedure statement
  • 38.
    RETURN STATEMENT• Syntax •RETURN expr DESCRIPTION • The RETURN statement terminates execution of a • stored function and returns the value expr to the • function caller. • NOTE: 1. There must be at least one RETURN statement in a stored function. 2. There may be more than one if the function has multiple exit points. This statement is not used in stored procedures or triggers.
  • 39.
    COMMIT OR ROLLBACK CONCEPTS •By default most database systems are configured to automatically COMMIT an SQL statement, i.e save it in the database. • Nevertheless this is possible in making transactions to either commit data to database or to roll back.
  • 40.
    SYNTAX OF ATRANSACTION START TRANSACTION [SQL statement(s)] COMMIT / ROLLBACK
  • 41.
  • 42.
  • 43.
  • 44.