3. Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
4. Stored Procedure
• An SP is a program code (like in a regular
computing language) stored in the database
server
• A stored procedure is a method to encapsulate
repetitive tasks
• Procedures are compiled and stored . So at run
time no need of compilation required which
makes pretty much faster
• stored procedures can reduce long SQL queries
to a single line
5. Creating a Stored Procedure
• DELIMITER //
CREATE PROCEDURE csp_procedure_name(<paramList>)
BEGIN
<procedure Body>;
END //
DELIMITER ;
–Keyword to create a procedure
Procedure Name as given by user
6. Creating a Stored Procedure
• DELIMITER //
CREATE PROCEDURE csp_procedure_name(<paramList>)
BEGIN
<procedure Body>;
END //
DELIMITER ;
A parameter in the paramList is specified as:
<mode> <param_name> <type>
Mode:
IN input parameter (default)
OUT output parameter
INOUT input and output parameter
• DELIMITER is character or string
user to tell MYSQL client that it
as an end of SQL statement
• By default ,the DELIMITER is
;(semi colon)
8. Deleting a stored procedure
• Syntax:
– DROP PROCEDURE procedureName;
9. Example
• DELIMITER //
• create procedure csp_insert_tbl_user_login(IN username varchar(200),IN
password varchar(100))
• begin
• DECLARE fk_int_id int;
• insert into tabl_user(vchar_username,vchar_password) values
(username,password);
• Set fk_int_id=last_insert_id();
• insert into tabl_login(fk_int_user_id) values(fk_int_id);
• end //
• DELIMITER ;
• Call csp_insert_user_login(”rahees”,”rahees123”);
10.
11. Cursor
• When we execute a statement, a relation is returned. It is
stored in private work area for the statement. Cursor is a
pointer to this area.
• A cursor lets you fetch one or more rows from a SQL result set
into stored program variables
• A cursor allows to iterate a set of rows returned by a query and
process each row accordingly.
• SELECT INTO is fine for single-row queries, but many
applications require the querying of multiple rows of data. You
will use a cursor in MySQL to accomplish this.
12. Declare Handler
• The DECLARE _____ HANDLER statement specifies a handler that deals with one
or more conditions.
• If one of these conditions occurs, the specified statement executes.
• DECLARE handler_action HANDLER FOR condition_value [, condition_value] ...
Statement
• handler_action:
• CONTINUE | EXIT | UNDO
• condition_value: mysql_error_code
– SQLWARNING | NOT FOUND | SQLEXCEPTION
13. Advantage
• Reuse the application logic
• do not need to write the code again and again
• code is stored in a pre-compiled form , does not need to
be compiled at run-time
• Reduced network traffic
• instead of sending multiple lengthy SQL statements, the
application has to send only name and parameters of
the stored procedure
• Call it whenever needed
16. Want to learn more about programming or Looking to become a good programmer?
Are you wasting time on searching so many contents online?
Do you want to learn things quickly?
Tired of spending huge amount of money to become a Software professional?
Do an online course
@ baabtra.com
We put industry standards to practice. Our structured, activity based courses are so designed
to make a quick, good software professional out of anybody who holds a passion for coding.
17. Follow us @ twitter.com/baabtra
Like us @ facebook.com/baabtra
Subscribe to us @ youtube.com/baabtra
Become a follower @ slideshare.net/BaabtraMentoringPartner
Connect to us @ in.linkedin.com/in/baabtra
Give a feedback @ massbaab.com/baabtra
Thanks in advance
www.baabtra.com | www.massbaab.com |www.baabte.com
18. Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Cafit Square,
Hilite Business Park,
Near Pantheerankavu,
Kozhikode
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com
Contact Us