SlideShare uma empresa Scribd logo
1 de 23
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
STORED PROCEDURE
WITH
CURSORS
Jaseena A P
jsnp65@gmail.com
www.facebook.com/Jaseena
Muhammed A P
twitter.com/username
in.linkedin.com/in/profilena
me
9539443588
WHAT IS STORED PROCEDURE?
 A stored procedure is a subroutine available to applications that
access a relational database system.
 Extensive or complex processing that requires execution of
several SQL statements is moved into stored procedures, and all
applications call the procedures.
 Typical uses for stored procedures include data
validation (integrated into the database) or access
control mechanisms.
 Stored procedures can consolidate and centralize logic that was
originally implemented in applications.
WHY WE USE STORED PROCEDURE
 Stored procedures should run faster
-Once created, stored procedures are compiled and stored in
the data base.
 Saving resources
-code is stored in a pre-compiled form ;syntactically valid and
does not need to be compiled again.
 Improving the scalability of applications
-each user of the stored procedure will use exactly the
same form of queries that means the code is reused.
WHY WE USE STORED PROCEDURE
 Less network traffic
-instead of sending multiple lengthy SQL
statements, the application has to send only name and
parameters of the stored procedure.
 Stored procedures are secure.
- Database administrator can grant
appropriate permissions to applications that access stored
procedures in the database without giving any permission on
the underlying database tables.
LIMITATIONS OF STORED PROCEDURE
 For a lot of stored procedures, the memory usage of every
connection will increase substantially.
 Overuse a large number of logical operations inside store
procedures, the CPU usage will also increase because database server
is not well-designed for logical operations.
 A constructs of stored procedures make it more difficult to develop
stored procedures that have complicated business logic.
 It is difficult to debug stored procedures. Only few database
management systems allow you to debug stored procedures
 It is not easy to develop and maintain stored procedures; Required
specialized skill set that not all application developers possess
Working with SP
Mysql> CREATE DATABASE my_db;
Mysql>USE my_db;
Mysql>CREATE TABLE Tbl_student(RollNo int,Name varchar(20));
Mysql> INSERT INTO Tbl_student VALUES (1,’Dilna’),(2,’Diya’),(3,’Dinan’);
Mysql>SELECT * FROM Tbl_student;
+--------+-------+
| RollNo | Name |
+--------+-------+
| 1 | Dilna |
| 2 | Diya |
| 3 | Dinan |
+--------+-------+
Working with SP
Musql>delimiter //
Mysql> CREATE PROCEDURE proc1 ()
BEGIN
DECLARE a INT;
SET a = 3;
INSERT INTO Tbl_student VALUES (5,’Name’);
SELECT * FROM Tbl_student WHERE RollNo=a;
END; //
Mysql>delimiter ;
Mysql>call proc1();
DEFAULT 0
+--------+-------+
| RollNo | Name |
+--------+-------+
| 3 | Dinan |
+--------+-------+
Working with SP-parameter passing
 CREATE PROCEDURE proc.Name() ...
 CREATE PROCEDURE proc.Name ([IN] name data-type) ...
 CREATE PROCEDURE proc.Name (OUT name data-type) ...
 CREATE PROCEDURE proc.Name (INOUT name data-type)
Working with SP-parameter passing
CREATE PROCEDURE proc1 (OUT count int)
BEGIN
DECLARE a INT;
SET a = 3;
INSERT INTO Tbl_student VALUES (5,’Name’);
SELECT count(RollNo) into countFROM Tbl_student ;
END; //
Mysql>call proc1(@count);
Mysql>select @count;
+--------+
| @count |
+--------+
| 4 |
+--------+
Conditions and If-then-else
Mysql>alter table Tbl_student add Grade varchar(10);
Mysql> delimiter //
Mysql> create procedure proc3(IN mrk int,IN rlno int)
BEGIN
if mrk>90 then
update Tbl_student set Grade=‘A’ where RollNo=rlno;
end if;
end//
Mysql> delimiter //
Mysql>call proc3(95,1);
Mysql>select * from Tbl_student;
+--------+-------+-------+
| RollNo | Name | Grade |
+--------+-------+-------+
| 1 | Dilna | A |
| 2 | Diya | NULL |
| 3 | Dinan | NULL |
| 5 | Name | NULL |
+--------+-------+-------+
CASE
Mysql>create procedure proc4(IN mrk int,IN rlno int)
begin
case mrk
when 90 then update Tbl_student set Grade=‘A’ where RollNo=rlno;
when 80 then update Tbl_student set Grade='B' where RollNo=rlno;
else update Tbl_student set Grade='C' where RollNo=rlno;
end case;
end;//
Mysql>call proc4(2,80);
Mysql>call proc5(3,70);
Mysql>select * from Tbl_student;
+--------+-------+-------+
| RollNo | Name | Grade |
+--------+-------+-------+
| 1 | Dilna | A |
| 2 | Diya | B |
| 3 | Dinan | C |
| 5 | Name | NULL |
+--------+-------+-------+
WHILE
mysql> create procedure proc6()
begin
declare v int;
set v=0;
create table student(count int);
while v<5 do
insert into student(count) values(v);
set v=v+1;
end while;
end;//
mysql> call proc6();
mysql> select * from student;
+-------+
| count |
+-------+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
+-------+
REPEAT
mysql>create procedure proc9()
begin
declare v int;
set v=0;
create table value(v1 int);
repeat
insert into value values (v);
set v=v+1;
until v>=5
end repeat;
end;//
mysql> call proc9();
mysql> select * from value;
+------+
| v1 |
+------+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
+------+
LOOP
CREATE PROCEDURE p16 ()
BEGIN
DECLARE v INT;
SET v = 0;
loop_label: LOOP
INSERT INTO t VALUES (v);
SET v = v + 1;
IF v >= 5 THEN
LEAVE loop_label;
END IF;
END LOOP;
END; //
The LEAVE statement means "exit the loop". The actual syntax of the
LEAVE statement is the word LEAVE and a statement label.
CURSOR IN MYSQL
 A cursor allows us to fetch one or more rows from a SQL result
set into stored program variables, usually with the intention of
performing some row-by-row processing on the result set.
 A cursor has the following properties:
 Asensitive: The server may or may not make a copy of its result
table
 Read only: Not updatable
 Nonscrollable: Can be traversed only in one direction and
cannot skip rows
HOW CURSOR WORKS?
CURSORS
create procedure my_proc(OUT return_val int)
begin
declare b int;
declare a int default 1;
declare mycur_1 cursor for select pk_student_id from Tbl_student;
declare continue handler for not found set a=0;
open mycur_1;
repeat
fetch mycur_1 into b;
set return_val=b;
until a=0
end repeat;
close mycur_1;
end;//
THANK YOU
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

Mais conteúdo relacionado

Mais procurados

Oracle 11g PL/SQL notes
Oracle 11g PL/SQL notesOracle 11g PL/SQL notes
Oracle 11g PL/SQL notesanilakduygu
 
PL-SQL DIFFERENT PROGRAMS
PL-SQL DIFFERENT PROGRAMSPL-SQL DIFFERENT PROGRAMS
PL-SQL DIFFERENT PROGRAMSraj upadhyay
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01Thuan Nguyen
 
Performance Instrumentation for PL/SQL: When, Why, How
Performance Instrumentation for PL/SQL: When, Why, HowPerformance Instrumentation for PL/SQL: When, Why, How
Performance Instrumentation for PL/SQL: When, Why, HowKaren Morton
 
Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Thuan Nguyen
 
3. writing MySql plugins for the information schema
3. writing MySql plugins for the information schema3. writing MySql plugins for the information schema
3. writing MySql plugins for the information schemaRoland Bouman
 
Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14Thuan Nguyen
 
Structured Query Language(SQL)
Structured Query Language(SQL)Structured Query Language(SQL)
Structured Query Language(SQL)PadmapriyaA6
 
Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13Thuan Nguyen
 
Functions oracle (pl/sql)
Functions oracle (pl/sql)Functions oracle (pl/sql)
Functions oracle (pl/sql)harman kaur
 

Mais procurados (19)

Oracle 11g PL/SQL notes
Oracle 11g PL/SQL notesOracle 11g PL/SQL notes
Oracle 11g PL/SQL notes
 
PL-SQL DIFFERENT PROGRAMS
PL-SQL DIFFERENT PROGRAMSPL-SQL DIFFERENT PROGRAMS
PL-SQL DIFFERENT PROGRAMS
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Module04
Module04Module04
Module04
 
Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11
 
Plsql
PlsqlPlsql
Plsql
 
Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01
 
Performance Instrumentation for PL/SQL: When, Why, How
Performance Instrumentation for PL/SQL: When, Why, HowPerformance Instrumentation for PL/SQL: When, Why, How
Performance Instrumentation for PL/SQL: When, Why, How
 
Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17
 
Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09
 
3. writing MySql plugins for the information schema
3. writing MySql plugins for the information schema3. writing MySql plugins for the information schema
3. writing MySql plugins for the information schema
 
Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14
 
Structured Query Language(SQL)
Structured Query Language(SQL)Structured Query Language(SQL)
Structured Query Language(SQL)
 
Cursors
CursorsCursors
Cursors
 
Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13
 
Functions oracle (pl/sql)
Functions oracle (pl/sql)Functions oracle (pl/sql)
Functions oracle (pl/sql)
 
PLSQL Tutorial
PLSQL TutorialPLSQL Tutorial
PLSQL Tutorial
 

Destaque (7)

Leniar datastructure
Leniar datastructureLeniar datastructure
Leniar datastructure
 
Android lifecycle
Android lifecycleAndroid lifecycle
Android lifecycle
 
Function creation,calling and passing arguments in c
Function creation,calling and passing arguments in cFunction creation,calling and passing arguments in c
Function creation,calling and passing arguments in c
 
Json
JsonJson
Json
 
Exception handling in .net
Exception handling in .netException handling in .net
Exception handling in .net
 
Control structures in c
Control structures in cControl structures in c
Control structures in c
 
Client&server side scripting
Client&server side scriptingClient&server side scripting
Client&server side scripting
 

Semelhante a Stored procedure with cursor

Sql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ISql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ICarlos Oliveira
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQLVikash Sharma
 
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu   (obscure) tools of the trade for tuning oracle sq lsTony Jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu (obscure) tools of the trade for tuning oracle sq lsInSync Conference
 
Stored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayiStored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayiMuhammed Thanveer M
 
Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management Systemsweetysweety8
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbmsjain.pralabh
 
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
Tony jambu   (obscure) tools of the trade for tuning oracle sq lsTony jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony jambu (obscure) tools of the trade for tuning oracle sq lsInSync Conference
 
Performance tuning a quick intoduction
Performance tuning   a quick intoductionPerformance tuning   a quick intoduction
Performance tuning a quick intoductionRiyaj Shamsudeen
 
MySQL sys schema deep dive
MySQL sys schema deep diveMySQL sys schema deep dive
MySQL sys schema deep diveMark Leith
 
Habits of Effective SAS Programmers
Habits of Effective SAS ProgrammersHabits of Effective SAS Programmers
Habits of Effective SAS ProgrammersSunil Gupta
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsmaxpane
 
Intro to tsql unit 14
Intro to tsql   unit 14Intro to tsql   unit 14
Intro to tsql unit 14Syed Asrarali
 
Web Cloud Computing SQL Server - Ferrara University
Web Cloud Computing SQL Server  -  Ferrara UniversityWeb Cloud Computing SQL Server  -  Ferrara University
Web Cloud Computing SQL Server - Ferrara Universityantimo musone
 

Semelhante a Stored procedure with cursor (20)

Sql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ISql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices I
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQL
 
Stored procedures with cursors
Stored procedures with cursorsStored procedures with cursors
Stored procedures with cursors
 
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu   (obscure) tools of the trade for tuning oracle sq lsTony Jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
 
Stored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayiStored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayi
 
Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management System
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
 
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
Tony jambu   (obscure) tools of the trade for tuning oracle sq lsTony jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
 
Aspects of 10 Tuning
Aspects of 10 TuningAspects of 10 Tuning
Aspects of 10 Tuning
 
Performance tuning a quick intoduction
Performance tuning   a quick intoductionPerformance tuning   a quick intoduction
Performance tuning a quick intoduction
 
MySQL sys schema deep dive
MySQL sys schema deep diveMySQL sys schema deep dive
MySQL sys schema deep dive
 
Store procedures
Store proceduresStore procedures
Store procedures
 
Habits of Effective SAS Programmers
Habits of Effective SAS ProgrammersHabits of Effective SAS Programmers
Habits of Effective SAS Programmers
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactions
 
lecture13.ppt
lecture13.pptlecture13.ppt
lecture13.ppt
 
Sherlock holmes for dba’s
Sherlock holmes for dba’sSherlock holmes for dba’s
Sherlock holmes for dba’s
 
PLSQL
PLSQLPLSQL
PLSQL
 
Intro to tsql
Intro to tsqlIntro to tsql
Intro to tsql
 
Intro to tsql unit 14
Intro to tsql   unit 14Intro to tsql   unit 14
Intro to tsql unit 14
 
Web Cloud Computing SQL Server - Ferrara University
Web Cloud Computing SQL Server  -  Ferrara UniversityWeb Cloud Computing SQL Server  -  Ferrara University
Web Cloud Computing SQL Server - Ferrara University
 

Mais de baabtra.com - No. 1 supplier of quality freshers

Mais de baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 

Último

TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 

Último (20)

TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 

Stored procedure with cursor

  • 1.
  • 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.
  • 4. STORED PROCEDURE WITH CURSORS Jaseena A P jsnp65@gmail.com www.facebook.com/Jaseena Muhammed A P twitter.com/username in.linkedin.com/in/profilena me 9539443588
  • 5. WHAT IS STORED PROCEDURE?  A stored procedure is a subroutine available to applications that access a relational database system.  Extensive or complex processing that requires execution of several SQL statements is moved into stored procedures, and all applications call the procedures.  Typical uses for stored procedures include data validation (integrated into the database) or access control mechanisms.  Stored procedures can consolidate and centralize logic that was originally implemented in applications.
  • 6. WHY WE USE STORED PROCEDURE  Stored procedures should run faster -Once created, stored procedures are compiled and stored in the data base.  Saving resources -code is stored in a pre-compiled form ;syntactically valid and does not need to be compiled again.  Improving the scalability of applications -each user of the stored procedure will use exactly the same form of queries that means the code is reused.
  • 7. WHY WE USE STORED PROCEDURE  Less network traffic -instead of sending multiple lengthy SQL statements, the application has to send only name and parameters of the stored procedure.  Stored procedures are secure. - Database administrator can grant appropriate permissions to applications that access stored procedures in the database without giving any permission on the underlying database tables.
  • 8. LIMITATIONS OF STORED PROCEDURE  For a lot of stored procedures, the memory usage of every connection will increase substantially.  Overuse a large number of logical operations inside store procedures, the CPU usage will also increase because database server is not well-designed for logical operations.  A constructs of stored procedures make it more difficult to develop stored procedures that have complicated business logic.  It is difficult to debug stored procedures. Only few database management systems allow you to debug stored procedures  It is not easy to develop and maintain stored procedures; Required specialized skill set that not all application developers possess
  • 9. Working with SP Mysql> CREATE DATABASE my_db; Mysql>USE my_db; Mysql>CREATE TABLE Tbl_student(RollNo int,Name varchar(20)); Mysql> INSERT INTO Tbl_student VALUES (1,’Dilna’),(2,’Diya’),(3,’Dinan’); Mysql>SELECT * FROM Tbl_student; +--------+-------+ | RollNo | Name | +--------+-------+ | 1 | Dilna | | 2 | Diya | | 3 | Dinan | +--------+-------+
  • 10. Working with SP Musql>delimiter // Mysql> CREATE PROCEDURE proc1 () BEGIN DECLARE a INT; SET a = 3; INSERT INTO Tbl_student VALUES (5,’Name’); SELECT * FROM Tbl_student WHERE RollNo=a; END; // Mysql>delimiter ; Mysql>call proc1(); DEFAULT 0 +--------+-------+ | RollNo | Name | +--------+-------+ | 3 | Dinan | +--------+-------+
  • 11. Working with SP-parameter passing  CREATE PROCEDURE proc.Name() ...  CREATE PROCEDURE proc.Name ([IN] name data-type) ...  CREATE PROCEDURE proc.Name (OUT name data-type) ...  CREATE PROCEDURE proc.Name (INOUT name data-type)
  • 12. Working with SP-parameter passing CREATE PROCEDURE proc1 (OUT count int) BEGIN DECLARE a INT; SET a = 3; INSERT INTO Tbl_student VALUES (5,’Name’); SELECT count(RollNo) into countFROM Tbl_student ; END; // Mysql>call proc1(@count); Mysql>select @count; +--------+ | @count | +--------+ | 4 | +--------+
  • 13. Conditions and If-then-else Mysql>alter table Tbl_student add Grade varchar(10); Mysql> delimiter // Mysql> create procedure proc3(IN mrk int,IN rlno int) BEGIN if mrk>90 then update Tbl_student set Grade=‘A’ where RollNo=rlno; end if; end// Mysql> delimiter // Mysql>call proc3(95,1); Mysql>select * from Tbl_student; +--------+-------+-------+ | RollNo | Name | Grade | +--------+-------+-------+ | 1 | Dilna | A | | 2 | Diya | NULL | | 3 | Dinan | NULL | | 5 | Name | NULL | +--------+-------+-------+
  • 14. CASE Mysql>create procedure proc4(IN mrk int,IN rlno int) begin case mrk when 90 then update Tbl_student set Grade=‘A’ where RollNo=rlno; when 80 then update Tbl_student set Grade='B' where RollNo=rlno; else update Tbl_student set Grade='C' where RollNo=rlno; end case; end;// Mysql>call proc4(2,80); Mysql>call proc5(3,70); Mysql>select * from Tbl_student; +--------+-------+-------+ | RollNo | Name | Grade | +--------+-------+-------+ | 1 | Dilna | A | | 2 | Diya | B | | 3 | Dinan | C | | 5 | Name | NULL | +--------+-------+-------+
  • 15. WHILE mysql> create procedure proc6() begin declare v int; set v=0; create table student(count int); while v<5 do insert into student(count) values(v); set v=v+1; end while; end;// mysql> call proc6(); mysql> select * from student; +-------+ | count | +-------+ | 0 | | 1 | | 2 | | 3 | | 4 | +-------+
  • 16. REPEAT mysql>create procedure proc9() begin declare v int; set v=0; create table value(v1 int); repeat insert into value values (v); set v=v+1; until v>=5 end repeat; end;// mysql> call proc9(); mysql> select * from value; +------+ | v1 | +------+ | 0 | | 1 | | 2 | | 3 | | 4 | +------+
  • 17. LOOP CREATE PROCEDURE p16 () BEGIN DECLARE v INT; SET v = 0; loop_label: LOOP INSERT INTO t VALUES (v); SET v = v + 1; IF v >= 5 THEN LEAVE loop_label; END IF; END LOOP; END; // The LEAVE statement means "exit the loop". The actual syntax of the LEAVE statement is the word LEAVE and a statement label.
  • 18. CURSOR IN MYSQL  A cursor allows us to fetch one or more rows from a SQL result set into stored program variables, usually with the intention of performing some row-by-row processing on the result set.  A cursor has the following properties:  Asensitive: The server may or may not make a copy of its result table  Read only: Not updatable  Nonscrollable: Can be traversed only in one direction and cannot skip rows
  • 20. CURSORS create procedure my_proc(OUT return_val int) begin declare b int; declare a int default 1; declare mycur_1 cursor for select pk_student_id from Tbl_student; declare continue handler for not found set a=0; open mycur_1; repeat fetch mycur_1 into b; set return_val=b; until a=0 end repeat; close mycur_1; end;//
  • 22. 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
  • 23. 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