Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
docu...
Week Target Achieved
1 30 25
2
3
Typing Speed
Jobs Applied
Week Company Designation Applied Date Current Status
1
2
3
WELCOME
Stored procedures and cursor
Midhun sudhakar
midhusudhakaran@gmail.com
Midhun.sudhakar.73@facebook.com
twitter.com/midhuno...
Content
• What is stored procedure.
• Advantages of stored procedure.
• What is Cursor.
• Sample stored procedure program.
What is stored procedure?
• Is a block of SQL statements.
• It can contain,
– variable definition.
– Conditional statement...
advantages
• Speed(pre-compiled execution).
• Reduce client/server traffic.
• Efficient re-use of code.
Syntax..
CREATE PROCEDURE procedure_name(parameter)
BEGIN
//definition;
END
Example..
CREATE PROCEDURE findcustomer(IN customer_id int)
BEGIN
select * from customer where id=customer_id;
END
CALL fi...
What is Cursor?
• cursor in stored procedures to iterate.
• through a result set returned by a SELECT
statement.
Syntax..
DECLARE cursor_name CURSOR FOR SELECT_statement;
OPEN cursor_name;
FETCH cursor_name INTO variables list;
CLOSE c...
Example..
DELIMITER $$
CREATE PROCEDURE build_email_list (INOUT email_list varchar(4000))
BEGIN
DECLARE v_finished INTEGER...
Ctnd..
OPEN email_cursor;
get_email: LOOP
FETCH email_cursor INTO v_email;
IF v_finished = 1 THEN
LEAVE get_email;
END IF;...
Write a single stored procedure to insert values
into both table?
DELIMITER $$
CREATE PROCEDURE double_insert( usr CHAR(12...
THANK TOU
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.co...
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, N...
Stored procedures and cursors
Upcoming SlideShare
Loading in …5
×

Stored procedures and cursors

385
-1

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
385
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Stored procedures and cursors

  1. 1. 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
  2. 2. Week Target Achieved 1 30 25 2 3 Typing Speed
  3. 3. Jobs Applied Week Company Designation Applied Date Current Status 1 2 3
  4. 4. WELCOME
  5. 5. Stored procedures and cursor Midhun sudhakar midhusudhakaran@gmail.com Midhun.sudhakar.73@facebook.com twitter.com/midhunopus in.linkedin.com/pub/midhunsudhakar/86/a65/a9b/Phonenumber 9995586182
  6. 6. Content • What is stored procedure. • Advantages of stored procedure. • What is Cursor. • Sample stored procedure program.
  7. 7. What is stored procedure? • Is a block of SQL statements. • It can contain, – variable definition. – Conditional statements. – Loops etc..
  8. 8. advantages • Speed(pre-compiled execution). • Reduce client/server traffic. • Efficient re-use of code.
  9. 9. Syntax.. CREATE PROCEDURE procedure_name(parameter) BEGIN //definition; END
  10. 10. Example.. CREATE PROCEDURE findcustomer(IN customer_id int) BEGIN select * from customer where id=customer_id; END CALL findcustomer(2);
  11. 11. What is Cursor? • cursor in stored procedures to iterate. • through a result set returned by a SELECT statement.
  12. 12. Syntax.. DECLARE cursor_name CURSOR FOR SELECT_statement; OPEN cursor_name; FETCH cursor_name INTO variables list; CLOSE cursor_name;
  13. 13. Example.. DELIMITER $$ CREATE PROCEDURE build_email_list (INOUT email_list varchar(4000)) BEGIN DECLARE v_finished INTEGER DEFAULT 0; DECLARE v_email varchar(100) DEFAULT ""; -- declare cursor for employee email DEClARE email_cursor CURSOR FOR SELECT email FROM employees; -- declare NOT FOUND handler DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1;
  14. 14. Ctnd.. OPEN email_cursor; get_email: LOOP FETCH email_cursor INTO v_email; IF v_finished = 1 THEN LEAVE get_email; END IF; -- build email list SET email_list = CONCAT(v_email,“,",email_list); END LOOP get_email; CLOSE email_cursor; END$$ SET @email_list = ""; CALL build_email_list(@email_list); SELECT @email_list;
  15. 15. Write a single stored procedure to insert values into both table? DELIMITER $$ CREATE PROCEDURE double_insert( usr CHAR(12), pass CHAR(12)) BEGIN START TRANSACTION; INSERT INTO tbl_user VALUES (usr,NULL); INSERT INTO tbl_login VALUES (NULL,usr, pass); COMMIT; END$$ DELIMITER ; SET @username = 'baabtra'; SET @password = '321'; CALL double_insert(@username,@password); SELECT * FROM tbl_user; SELECT * FROM tbl_login;
  16. 16. THANK TOU
  17. 17. If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  18. 18. Contact Us 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 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com IKK Road, East Hill, Kozhikode Kerala, India. Ph: + 91 – 495 30 63 624 NIT-TBI, NIT Campus, Kozhikode, Kerala, India.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×