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. What is a Stored procedure??
• Segment of sql code
• Stored in database catalog
• Can called later
5. Advantages of stored procedure
• Increases performance of application
• Reduced traffic
• Secure
6. How we create a stored
procedure??
delimiter //
create procedure csp_insert_tbl_user_role(in role varchar(20))
begin
insert into tbl_user_role values(null,role);
end //
delimiter ;
7. How to call a stored procedure??
call csp_insert_tbl_user_role(null,"employee");
8. Cursor
• Why we use cursor ??
To handle a result set inside a stored procedure
• A cursor allow you to iterate a set of rows
returned by a query and process each row
accordingly
• mySQL cursor is read only,non-scrollable
9. Working with mySQL cursor
1. DECLARE cursor_name CURSOR FOR
SELECT_statement;
2. OPEN cursor_name;
3. FETCH cursor_name INTO variables list;
4. CLOSE cursor_name;
10. NOT FOUND Handler
• When we use NOT FOUNF handler?
Syntax
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET finished = 1;
12. Example• delimiter //
• Create procedure csp_cur_check_age()
• Begin
• declare id int;
• DECLARE diff int;
• DECLARE done INT DEFAULT 0;
• DECLARE myCur CURSOR FOR SELECT pk_int_teacher_id,timestampdiff(year,dat_teacher_doj,curdate()) from tbl_teachers;
• DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
• OPEN myCur;
• teacher_Loop: LOOP
• FETCH myCur INTO id,diff;
• if diff>32 then
• update tbl_teachers set bln_teacher_status=0 where pk_int_teacher_id=id;
• end if;
• If done=1 THEN
• LEAVE teacher_loop;
• else
• iterate teacher_loop;
• END IF;
• END LOOP teacher_loop;
• CLOSE myCur ;
• select vchr_teacher_name,bln_teacher_status from tbl_teachers;
• END//
• delimiter ;
13. Problem 1
• Create 2 tables tbl_login,tbl_user
Write a single stored procedure to insert values into both table
Solution
Delimiter //
Create procedure csp_insert(in username varchar(20),in pass varchar(10),in firstname
varchar(20),in lastname varchar(20))
Begin
declare fk_login int;
Insert into tbl_login values(null,username,pass);
select last_insert_id()into fk_login;
insert into tbl_users values(null,firstname,lastname,fk_login);
end //
delimiter ;
call csp_insert("yasirmusthafa","yasir123","yasir","musthafa");
15. 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.
16. 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
17. 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