Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Yasirmusthafa pp
yasirmusthafapp@gmail.com
www.facebook.com/yasirmusthafa
twitter.com/yasirmusthafapp
in.linkedin.com/in/y...
Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
docu...
What is a Stored procedure??
• Segment of sql code
• Stored in database catalog
• Can called later
Advantages of stored procedure
• Increases performance of application
• Reduced traffic
• Secure
How we create a stored
procedure??
delimiter //
create procedure csp_insert_tbl_user_role(in role varchar(20))
begin
inser...
How to call a stored procedure??
call csp_insert_tbl_user_role(null,"employee");
Cursor
• Why we use cursor ??
To handle a result set inside a stored procedure
• A cursor allow you to iterate a set of ro...
Working with mySQL cursor
1. DECLARE cursor_name CURSOR FOR
SELECT_statement;
2. OPEN cursor_name;
3. FETCH cursor_name IN...
NOT FOUND Handler
• When we use NOT FOUNF handler?
Syntax
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET finished = 1;
Flow chart
Example• delimiter //
• Create procedure csp_cur_check_age()
• Begin
• declare id int;
• DECLARE diff int;
• DECLARE done ...
Problem 1
• Create 2 tables tbl_login,tbl_user
Write a single stored procedure to insert values into both table
Solution
D...
THANK YOU
Want to learn more about programming or Looking to become a good programmer?
Are you wasting time on searching so many con...
Follow us @ twitter.com/baabtra
Like us @ facebook.com/baabtra
Subscribe to us @ youtube.com/baabtra
Become a follower @ s...
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Sta...
Stored procedure
Upcoming SlideShare
Loading in …5
×

Stored procedure

439 views

Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

Stored procedure

  1. 1. Yasirmusthafa pp yasirmusthafapp@gmail.com www.facebook.com/yasirmusthafa twitter.com/yasirmusthafapp in.linkedin.com/in/yasirmusthafapp 8891396749 Stored procedure
  2. 2. 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
  3. 3. What is a Stored procedure?? • Segment of sql code • Stored in database catalog • Can called later
  4. 4. Advantages of stored procedure • Increases performance of application • Reduced traffic • Secure
  5. 5. 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 ;
  6. 6. How to call a stored procedure?? call csp_insert_tbl_user_role(null,"employee");
  7. 7. 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
  8. 8. 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;
  9. 9. NOT FOUND Handler • When we use NOT FOUNF handler? Syntax DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
  10. 10. Flow chart
  11. 11. 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 ;
  12. 12. 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");
  13. 13. THANK YOU
  14. 14. 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.
  15. 15. 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
  16. 16. 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

×