Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views111 pages

Introduction To Stored Routines

Uploaded by

Patel Saikiran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views111 pages

Introduction To Stored Routines

Uploaded by

Patel Saikiran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 111

Stored Routines

Introduction to Stored Routines


Introduction to Stored Routines

routine (in a context other than computer science)

a usual, fixed action, or series of actions, repeated periodically


Introduction to Stored Routines

query output
Introduction to Stored Routines
Introduction to Stored Routines

stored routine
an SQL statement, or a set of SQL statements, that can be stored on the
database server

- whenever a user needs to run the query in question, they can


call, reference, or invoke the routine
Introduction to Stored Routines

algorithm
1) checks all monthly sales
generated throughout a
query calendar year
2) returns the lowest of these
values

stored routine
Introduction to Stored Routines

= 100
Introduction to Stored Routines

stored routine
Introduction to Stored Routines

stored routine
Introduction to Stored Routines

stored routine

= 100 - this routine can bring the desired result multiple times
Introduction to Stored Routines

stored routines

stored procedures functions


= procedures = user-defined
functions

≠ built-in functions
(aggregate functions,
datetime functions)
Introduction to Stored Routines

stored routines

stored procedures functions


The MySQL Syntax for Stored Procedures
The MySQL Syntax for Stored Procedures
semi-colons ;
- they function as a statement terminator
- technically, they can also be called delimiters

- by typing DELIMITER $$, you’ll be able to use the dollar symbols as


your delimiter

DELIMITER $$
The MySQL Syntax for Stored Procedures

Query #1 ;
Query #2 ;
call stored_procedure_1 ;
p_Query #1 ;

p_Query #2 ;

stored procedure
The MySQL Syntax for Stored Procedures

Query #1 ;
Query #2 ;
call stored_procedure_1 ;
p_Query #1 ;

p_Query #2 ;
Query #4 ;
Query #5 ;
… ; stored procedure
The MySQL Syntax for Stored Procedures

Query #1 ; $$ or //

Query #2 ;
DELIMITER $$
call stored_procedure_1 ;
p_Query #1 ;

p_Query #2 ;

stored procedure
The MySQL Syntax for Stored Procedures

Query #1 ;
Query #2 ;
DELIMITER $$
call stored_procedure_1 ;
p_Query #1 ;

p_Query #2 ;
Query #4 ;
Query #5 ;
… ; stored procedure
The MySQL Syntax for Stored Procedures

DELIMITER $$
CREATE PROCEDURE procedure_name()
The MySQL Syntax for Stored Procedures

DELIMITER $$
CREATE PROCEDURE procedure_name(param_1, param_2)

Parameters represent certain


values that the procedure will
use to complete the calculation
it is supposed to execute
The MySQL Syntax for Stored Procedures

DELIMITER $$
CREATE PROCEDURE procedure_name() A procedure can be created
without parameters too!
Nevertheless, the parentheses
must always be attached to
its name
The MySQL Syntax for Stored Procedures

DELIMITER $$
CREATE PROCEDURE procedure_name()
BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
The MySQL Syntax for Stored Procedures

DELIMITER $$
CREATE PROCEDURE procedure_name()
BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
body of the procedure
The MySQL Syntax for Stored Procedures

DELIMITER $$
CREATE PROCEDURE procedure_name()
BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
The MySQL Syntax for Stored Procedures

DELIMITER $$
CREATE PROCEDURE procedure_name()
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
The MySQL Syntax for Stored Procedures

DELIMITER $$
CREATE PROCEDURE procedure_name()
+ BEGIN
The MySQL Syntax for Stored Procedures

DELIMITER $$
CREATE PROCEDURE procedure_name()
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
The MySQL Syntax for Stored Procedures

DELIMITER $$
CREATE PROCEDURE procedure_name()
- BEGIN
SELECT * FROM employees
LIMIT 1000;
query
END$$
The MySQL Syntax for Stored Procedures

DELIMITER $$
CREATE PROCEDURE procedure_name()
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
;
The MySQL Syntax for Stored Procedures

DELIMITER $$
CREATE PROCEDURE procedure_name()
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
;
The MySQL Syntax for Stored Procedures

DELIMITER $$
CREATE PROCEDURE procedure_name()
- BEGIN
SELECT * FROM employees
LIMIT 1000$$
END$$
The MySQL Syntax for Stored Procedures

DELIMITER $$
CREATE PROCEDURE procedure_name()
- BEGIN
SELECT * FROM employees
LIMIT 1000$$
END$$
The MySQL Syntax for Stored Procedures

DELIMITER $$
CREATE PROCEDURE procedure_name()
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
The MySQL Syntax for Stored Procedures

DELIMITER $$
CREATE PROCEDURE procedure_name()
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
DELIMITER ;
The MySQL Syntax for Stored Procedures

DELIMITER $$
CREATE PROCEDURE procedure_name()
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
From this moment on, $$ will not act as a
DELIMITER ; delimiter
Stored Procedures with an Input Parameter
Stored Procedures with an Input Parameter
a stored routine can perform a calculation that transforms an input
value in an output value
Stored Procedures with an Input Parameter
a stored routine can perform a calculation that transforms an input
value in an output value
stored procedures can take an input value and then use it in the query,
or queries, written in the body of the procedure
Stored Procedures with an Input Parameter
a stored routine can perform a calculation that transforms an input
value in an output value
stored procedures can take an input value and then use it in the query,
or queries, written in the body of the procedure

- this value is represented by the IN parameter


Stored Procedures with an Input Parameter
a stored routine can perform a calculation that transforms an input
value in an output value
stored procedures can take an input value and then use it in the query,
or queries, written in the body of the procedure

- this value is represented by the IN parameter


Stored Procedures with an Input Parameter
a stored routine can perform a calculation that transforms an input
value in an output value
stored procedures can take an input value and then use it in the query,
or queries, written in the body of the procedure

- this value is represented by the IN parameter


Stored Procedures with an Input Parameter
a stored routine can perform a calculation that transforms an input
value in an output value
stored procedures can take an input value and then use it in the query,
or queries, written in the body of the procedure

- this value is represented by the IN parameter


Stored Procedures with an Input Parameter

DELIMITER $$
CREATE PROCEDURE procedure_name(in parameter)
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
DELIMITER ;
Stored Procedures with an Input Parameter
a stored routine can perform a calculation that transforms an input
value in an output value
stored procedures can take an input value and then use it in the query,
or queries, written in the body of the procedure

- this value is represented by the IN parameter


Stored Procedures with an Input Parameter
a stored routine can perform a calculation that transforms an input
value in an output value
stored procedures can take an input value and then use it in the query,
or queries, written in the body of the procedure

- this value is represented by the IN parameter

- after that calculation is ready, a result will be returned


Stored Procedures with an Output Parameter
Stored Procedures with an Output Parameter

DELIMITER $$
CREATE PROCEDURE procedure_name(in parameter, out parameter)
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
DELIMITER ;
Stored Procedures with an Output Parameter

DELIMITER $$
CREATE PROCEDURE procedure_name(in parameter, out parameter)
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
DELIMITER ;
Stored Procedures with an Output Parameter

DELIMITER $$
CREATE PROCEDURE procedure_name(in parameter, out parameter)
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
DELIMITER ;
Stored Procedures with an Output Parameter

DELIMITER $$
CREATE PROCEDURE procedure_name(in parameter, out parameter)
- BEGIN
SELECT * FROM employees
it will represent the variable
LIMIT 1000; containing the output value of
the operation executed by the
END$$
query of the stored procedure
DELIMITER ;
Stored Procedures with an Output Parameter
every time you create a procedure containing both an IN and an OUT
parameter, remember that you must use the SELECT-INTO structure in the
query of this object’s body!
Variables
Variables
when you are defining a program, such as a stored procedure for
instance, you can say you are using ‘parameters’
Variables
when you are defining a program, such as a stored procedure for
instance, you can say you are using ‘parameters’
- ‘parameters’ are a more abstract term
Variables
when you are defining a program, such as a stored procedure for
instance, you can say you are using ‘parameters’
- ‘parameters’ are a more abstract term

DELIMITER $$
CREATE PROCEDURE procedure_name (in , out )
Variables
when you are defining a program, such as a stored procedure for
instance, you can say you are using ‘parameters’
- ‘parameters’ are a more abstract term

DELIMITER $$
CREATE PROCEDURE procedure_name (in parameter , out )
Variables
when you are defining a program, such as a stored procedure for
instance, you can say you are using ‘parameters’
- ‘parameters’ are a more abstract term

DELIMITER $$
CREATE PROCEDURE procedure_name (in parameter , out parameter )
Variables
once the structure has been solidified, then it will be applied to the
database. The input value you insert is typically referred to as the
‘argument’, while the obtained output value is stored in a ‘variable’
Variables
once the structure has been solidified, then it will be applied to the
database. The input value you insert is typically referred to as the
‘argument’, while the obtained output value is stored in a ‘variable’

CREATE PROCEDURE …
Variables
once the structure has been solidified, then it will be applied to the
database. The input value you insert is typically referred to as the
‘argument’, while the obtained output value is stored in a ‘variable’

input:

CREATE PROCEDURE … argument


Variables
once the structure has been solidified, then it will be applied to the
database. The input value you insert is typically referred to as the
‘argument’, while the obtained output value is stored in a ‘variable’

input:

CREATE PROCEDURE … argument


Variables
once the structure has been solidified, then it will be applied to the
database. The input value you insert is typically referred to as the
‘argument’, while the obtained output value is stored in a ‘variable’

input: output:

CREATE PROCEDURE … argument variable


Variables

DELIMITER $$
CREATE PROCEDURE procedure_name (in parameter , out parameter )


input: output:

CREATE PROCEDURE … argument variable


Variables
IN-OUT parameters

CREATE PROCEDURE …
Variables
IN-OUT parameters

input:

CREATE PROCEDURE … IN parameter


Variables
IN-OUT parameters

input:

CREATE PROCEDURE … IN parameter


Variables
IN-OUT parameters

input:

CREATE PROCEDURE … OUT parameter


Variables
IN-OUT parameters

input = output

CREATE PROCEDURE … OUT parameter


User-Defined Functions in MySQL
User-Defined Functions in MySQL

DELIMITER $$
CREATE FUNCTION function_name(parameter data_type) RETURNS data_type
DECLARE variable_name data_type
- BEGIN
SELECT …
RETURN variable_name
END$$
DELIMITER ;
User-Defined Functions in MySQL

DELIMITER $$
CREATE FUNCTION function_name(parameter data_type) RETURNS data_type
DECLARE variable_name data_type
- BEGIN here you have no OUT parameters to
SELECT … define between the parentheses after
the object’s name
RETURN variable_name
END$$
DELIMITER ;
User-Defined Functions in MySQL

DELIMITER $$
CREATE FUNCTION function_name(parameter data_type) RETURNS data_type
DECLARE variable_name data_type
- BEGIN here you have no OUT parameters to
SELECT … define between the parentheses after
the object’s name
RETURN variable_name
all parameters are IN, and since this
END$$ is well known, you need not explicitly
indicate it with the word, ‘IN’
DELIMITER ;
User-Defined Functions in MySQL

DELIMITER $$
CREATE FUNCTION function_name(parameter data_type) RETURNS data_type
DECLARE variable_name data_type
- BEGIN
although there are no OUT
SELECT … parameters, there is a
RETURN variable_name ‘return value’

END$$
DELIMITER ;
User-Defined Functions in MySQL

DELIMITER $$
CREATE FUNCTION function_name(parameter data_type) RETURNS data_type
DECLARE variable_name data_type
- BEGIN
although there are no OUT
SELECT … parameters, there is a
RETURN variable_name ‘return value’
it is obtained after running the
END$$
query contained in the body of
DELIMITER ; the function
User-Defined Functions in MySQL

DELIMITER $$
CREATE FUNCTION function_name(parameter data_type) RETURNS data_type
DECLARE variable_name data_type
- BEGIN it can be of any data type
SELECT …
RETURN variable_name
END$$
DELIMITER ;
User-Defined Functions in MySQL
we cannot call a function!
User-Defined Functions in MySQL
we cannot call a function!
we can select it, indicating an input value within parentheses
User-Defined Functions in MySQL
we cannot call a function!
we can select it, indicating an input value within parentheses

SELECT function_name(input_value);
Stored Routines - Conclusion
Stored Routines - Conclusion

TECHNICAL DIFFERENCES
Stored Routines - Conclusion

TECHNICAL DIFFERENCES

stored procedure
Stored Routines - Conclusion

TECHNICAL DIFFERENCES

stored procedure
Stored Routines - Conclusion

TECHNICAL DIFFERENCES

user-defined
stored procedure function
Stored Routines - Conclusion

TECHNICAL DIFFERENCES

user-defined
stored procedure function

returns a value
Stored Routines - Conclusion

TECHNICAL DIFFERENCES

user-defined
stored procedure function

does not return a returns a value


value
Stored Routines - Conclusion

TECHNICAL DIFFERENCES

user-defined
stored procedure function

does not return a returns a value


value

CALL procedure;
Stored Routines - Conclusion

TECHNICAL DIFFERENCES

user-defined
stored procedure function

does not return a returns a value


value

CALL procedure; SELECT function;


Stored Routines - Conclusion

CONCEPTUAL DIFFERENCES
Stored Routines - Conclusion

CONCEPTUAL DIFFERENCES

user-defined
stored procedure function
Stored Routines - Conclusion

CONCEPTUAL DIFFERENCES

user-defined
stored procedure function

can have multiple OUT


parameters
Stored Routines - Conclusion

CONCEPTUAL DIFFERENCES

user-defined
stored procedure function

can have multiple OUT can return a single


parameters value only
Stored Routines - Conclusion

CONCEPTUAL DIFFERENCES

user-defined
stored procedure function

can have multiple OUT can return a single


parameters value only

- if you need to obtain more than one value as a result of a


calculation, you are better off using a procedure
Stored Routines - Conclusion

CONCEPTUAL DIFFERENCES

user-defined
stored procedure function

can have multiple OUT can return a single


parameters value only

- if you need to obtain more than one value as a result of a


calculation, you are better off using a procedure
- if you need to just one value to be returned, then you can use a
function
Stored Routines - Conclusion
how about involving an INSERT, an UPDATE, or a DELETE statement?
Stored Routines - Conclusion
how about involving an INSERT, an UPDATE, or a DELETE statement?

- in those cases, the operation performed will apply changes to the data in your
database
Stored Routines - Conclusion
how about involving an INSERT, an UPDATE, or a DELETE statement?

- in those cases, the operation performed will apply changes to the data in your
database
- there will be no value, or values, to be returned and displayed to the user
Stored Routines - Conclusion

CONCEPTUAL DIFFERENCES

user-defined
stored procedure function

can have multiple OUT can return a single


parameters value only
Stored Routines - Conclusion

CONCEPTUAL DIFFERENCES

user-defined
stored procedure function

can have multiple OUT can return a single


parameters value only

INSERT UPDATE

DELETE
Stored Routines - Conclusion

CONCEPTUAL DIFFERENCES

user-defined
stored procedure function

can have multiple OUT can return a single


parameters value only

INSERT UPDATE

DELETE
Stored Routines - Conclusion

CONCEPTUAL DIFFERENCES

user-defined
stored procedure function

can have multiple OUT can return a single


parameters value only

INSERT UPDATE INSERT UPDATE

DELETE DELETE
Stored Routines - Conclusion

CONCEPTUAL DIFFERENCES

user-defined
stored procedure function

can have multiple OUT can return a single


parameters value only

INSERT UPDATE INSERT UPDATE

DELETE DELETE
Stored Routines - Conclusion

user-defined
stored procedure function
Stored Routines - Conclusion

user-defined
stored procedure function

TECHNICAL DIFFERENCE
Stored Routines - Conclusion

user-defined
stored procedure function

TECHNICAL DIFFERENCE

CALL procedure; SELECT function;


Stored Routines - Conclusion

user-defined
stored procedure function

TECHNICAL DIFFERENCE

CALL procedure; SELECT function;

CONCEPTUAL DIFFERENCE
Stored Routines - Conclusion

user-defined
stored procedure function

TECHNICAL DIFFERENCE

CALL procedure; SELECT function;

CONCEPTUAL DIFFERENCE
- you can easily include
a function as one of the
columns inside a SELECT
statement
Stored Routines - Conclusion

user-defined
stored procedure function

TECHNICAL DIFFERENCE

CALL procedure; SELECT function;

CONCEPTUAL DIFFERENCE
- including a procedure - you can easily include
in a SELECT statement a function as one of the
is impossible columns inside a SELECT
statement
Stored Routines - Conclusion
once you become an advanced SQL user, and have gained a lot of practice,
you will appreciate the advantages and disadvantages of both types of
programs
Stored Routines - Conclusion
once you become an advanced SQL user, and have gained a lot of practice,
you will appreciate the advantages and disadvantages of both types of
programs
- you will encounter many cases where you should choose between procedures and
functions
Stored Routines - Conclusion
what we did in this section was to lay the foundation of the relevant
syntax, as well as performing exercises on the practical aspects of
these tools

You might also like