SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

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
 

Tendances (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
 

En vedette (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
 

Similaire à 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
 

Similaire à 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
 

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

Plus 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
 

Dernier

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 

Dernier (20)

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 

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