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.

Stored procedures with cursor


Published on


  • Be the first to comment

  • Be the first to like this

Stored procedures with cursor

  1. 1. Raheescv 09633155669 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. 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
  4. 4. 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
  5. 5. 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)
  6. 6. Calling a stored procedure • Syntax: – CALL stored_procedure_name (param1, param2, ....);
  7. 7. Deleting a stored procedure • Syntax: – DROP PROCEDURE procedureName;
  8. 8. 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”);
  9. 9. 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.
  10. 10. 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
  11. 11. 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
  12. 12. ANY DOUBTS??
  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 @ 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 @ Like us @ Subscribe to us @ Become a follower @ Connect to us @ Give a feedback @ Thanks in advance | |
  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: Contact Us