• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Stored procedures and cursors
 
  • 333 views

 

Statistics

Views

Total Views
333
Views on SlideShare
333
Embed Views
0

Actions

Likes
0
Downloads
4
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Stored procedures and cursors Stored procedures and cursors Presentation Transcript

    • 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
    • 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/midhunopus in.linkedin.com/pub/midhunsudhakar/86/a65/a9b/Phonenumber 9995586182
    • 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 statements. – Loops etc..
    • 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 findcustomer(2);
    • 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 cursor_name;
    • 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;
    • 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;
    • 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;
    • THANK TOU
    • 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
    • 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.