SlideShare a Scribd company logo
1 of 34
STORED PROCEDURE
STORED PROCEDURE
 A procedure is a subroutine (like a subprogram) in a
regular scripting language, stored in a database
 In the case of MySQL, procedures are written in MySQL
and stored in the MySQL database/server
 A MySQL procedure has a name, a parameter list, and
SQL statement(s)
STORED PROCEDURES
 Database program modules that are stored and
executed by the DBMS at the server
DELIMITER //
CREATE PROCEDURE GetAllProducts()
BEGIN
SELECT * FROM products;
END //
DELIMITER ;
WHY STORED PROCEDURES
 Reduces Duplication of effort and improves software modularity
 Multiple applications can use the stored procedure vs. the SQL
statements being stored in the application language (PHP)
Reduces communication and data transfer cost between
client and server (in certain situations)
 Instead of sending multiple lengthy SQL statements, the
application only has to send the name and parameters of the
Stored Procedure
Can be more secure than SQL statements
 Permission can be granted to certain stored procedures without
granting access to database tables
DISADVANTAGES OF STORED PROCEDURES
 Difficult to debug
 MySQL does not provide ways for debugging stored
procedures
Many stored procedures can increase memory use
 The more stored procedures you use, the more memory is
used
Can be difficult to maintain and develop stored procedures
 Another programming language to learn
CREATING STORED PROCEDURES
DELIMITER //
CREATE PROCEDURE NAME
BEGIN
SQL STATEMENT
END //
DELIMITER ;
DELIMITER //
CREATE PROCEDURE GetAllProducts()
BEGIN
SELECT * FROM products;
END //
DELIMITER ;
CALLING STORED PROCEDURES
CALL
STORED_PROCEDURE_NAME
CALL GetAllProducts();
TYPES OF MYSQL PROCEDURES:
1. Procedure with no parameters:
2. Procedure with IN parameter:
3. Procedure with OUT parameter:
4. Procedure with IN-OUT parameter:
1. PROCEDURE WITH NO PARAMETERS:
A procedure without parameters does not
take any input or casts an output
indirectly.
It is simply called with its procedure
name followed by () (without any
parameters).
It is used for simple queries.
EXAMPLE PROCEDURE WITH NO PARAMETERS
delimiter //
create procedure display_book()
-> begin
-> select *from book;
-> end //
call display_book(); //
PARAMETERS
 There may be times where you want to pass
information to the stored procedures
 Getting user input from a form and using that input in a
SQL statement
PROCEDURE WITH IN PARAMETER:
 An IN parameter is used to take a parameter as
input such as an attribute
 When we define an IN parameter in a procedure,
the calling program has to pass an argument to the
stored procedure
 The value of an IN parameter is protected
EXAMPLE PROCEDURE WITH IN PARAMETER:
delimiter //
create procedure update_price
(IN temp_ISBN varchar(10), IN new_price
integer)
-> begin
-> update book set price=new_price
-> where ISBN=temp_ISBN;
-> end; //
call update_price(001, 600); //
PROCEDURE WITH OUT PARAMETER:
 An OUT parameter is used to pass a parameter as
output or display like the select operator, but
implicitly (through a set value)
 The value of an OUT parameter can be changed
inside the procedure and its new value is passed
back to the calling program
 A procedure cannot access the initial value of the
OUT parameter when it starts.
EXAMPLE ON PROCEDURE WITH OUT
PARAMETER:
delimiter //
create procedure
disp_max(OUT highestprice integer)
-> begin
-> select max(price) into
highestprice from book;
-> end; //
call disp_max(@M); //
select @M;
PROCEDURE WITH IN-OUT PARAMETER:
 An INOUT parameter is a combination of IN and OUT
parameters
 It means that the calling program may pass the
argument, and the stored procedure can modify the
INOUT parameter and pass the new value back to the
calling program.
EXAMPLE ON PROCEDURE WITH IN-OUT PARAMETER
ARGUMENTS AND PARAMETERS
DELIMITER //
CREATE PROCEDURE GetOfficeByCountry(IN countryName VARCHAR(25
BEGIN
SELECT * FROM offices WHERE country = countryName;
END //
DELIMITER ;
Defining
Calling
CALL GetOfficeByCountry('USA')
The values being copied from the calling stored procedure are calling
arguments.
The variables being copied into are called parameters.
OUT PARAMETER
DELIMITER //
CREATE PROCEDURE CountOrderByStatus(IN orderStatus
VARCHAR(25), OUT total INT)
BEGIN
SELECT count(orderNumber) INTO total FROM orders WHERE status =
orderStatus;
END//
DELIMITER ;
Defining
Calling
CALL CountOrderByStatus('Shipped',@total);
SELECT @total;
The out parameter is used outside of the stored
procedure.
VARIABLES
 A variable is a name that refers to a value
A name that represents a value stored in the
computer memory
C program
int name=“Amit”;
int age=25
 MySQL
DECLARE name VARCHAR(255)
DECLARE age INT
DELIMITER //
CREATE PROCEDURE declare2(IN value
int,IN name varchar(20))
BEGIN
DECLARE x INT;
DECLARE str VARCHAR(255);
SET x = value;
SET str = name;
SELECT x,str;
END//
DELIMITER ;
CONDITIONALS
THE “IF” STATEMENT
Mysql Syntax
IF if_expression THEN commands
[ELSEIF elseif_expression THEN commands]
[ELSE commands]
END IF;
First line is known as the IF clause
Includes the keyword IF followed by condition followed by the
keyword THEN
 When the IFstatement executes, the condition is tested,
and if it is true the block statements are executed.
Otherwise, block statements are skipped
“IFEXPRESSION”: BOOLEAN EXPRESSIONS AND
OPERATORS
IF STATEMENT
DELIMITER //
CREATE PROCEDURE
GetProductsInStockBasedOnQuantitityLevel(IN p_operator
VARCHAR(255), IN p_quantityInStock INT)
BEGIN
IF p_operator = "<" THEN
select * from products WHERE quantityInStock <
p_quantityInStock;
ELSEIF p_operator = ">" THEN
select * from products WHERE quantityInStock >
p_quantityInStock;
END IF;
END //
DELIMITER ;
IF STATEMENT
 CREATE PROCEDURE
GetProductsInStockBasedOnQuantitityLevel
(IN p_operator VARCHAR(255), IN p_quantityInStock
INT)
The ooperator > or < The number in stock
THE IF STATEMENT
IF p_operator = "<" THEN
select * from products WHERE quantityInStock <
p_quantityInStock;
ELSEIF p_operator = ">" THEN
select * from products WHERE quantityInStock >
p_quantityInStock;
END IF;
LOOPS
 While
 Repeat
 Loop
Repeats a set of commands until some condition
is met
Iteration: one execution of the body of a loop
If a condition is never met, we will have an infinite
loop
WHILE LOOP
WHILE expression DO
Statements
END WHILE
The expression must evaluate
to true or false
while loop is known as a pretest loop
Tests condition before performing an iteration
Will never execute if condition is false to start with
Requires performing some steps prior to the loop
INFINITE LOOPS
 Loops must contain within themselves a way to
terminate
 Something inside a while loop must eventually make
the condition false
 Infinite loop: loop that does not have a way of
stopping
 Repeats until program is interrupted
 Occurs when programmer forgets to include
stopping code in the loop
WHILE LOOP
DELIMITER //
CREATE PROCEDURE WhileLoopProc()
BEGIN
DECLARE x INT;
DECLARE str VARCHAR(255);
SET x = 1;
SET str = '';
WHILE x <= 5 DO
SET str = CONCAT(str,x,',');
SET x = x + 1;
END WHILE;
SELECT str;
END//
DELIMITER ;
WHILE LOOP
 Creating Variables
DECLARE x INT;
DECLARE str VARCHAR(255);
SET x = 1;
SET str = '';
WHILE LOOP
WHILE x <= 5 DO
SET str = CONCAT(str,x,',');
SET x = x + 1;
END WHILE;

More Related Content

What's hot (20)

Data Persistence in Android with Room Library
Data Persistence in Android with Room LibraryData Persistence in Android with Room Library
Data Persistence in Android with Room Library
 
Sql views
Sql viewsSql views
Sql views
 
MySQL Transactions
MySQL TransactionsMySQL Transactions
MySQL Transactions
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Generics
GenericsGenerics
Generics
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 
Introduction to oop
Introduction to oop Introduction to oop
Introduction to oop
 
Flask – Python
Flask – PythonFlask – Python
Flask – Python
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
Oop java
Oop javaOop java
Oop java
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in JAVA
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQL
 

Similar to Stored procedures (20)

Module04
Module04Module04
Module04
 
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
 
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
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
 
Introduction to mysql part 3
Introduction to mysql part 3Introduction to mysql part 3
Introduction to mysql part 3
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
stored.ppt
stored.pptstored.ppt
stored.ppt
 
Db Triggers05ch
Db Triggers05chDb Triggers05ch
Db Triggers05ch
 
Thinking Beyond ORM in JPA
Thinking Beyond ORM in JPAThinking Beyond ORM in JPA
Thinking Beyond ORM in JPA
 
2310 b 11
2310 b 112310 b 11
2310 b 11
 
Procedure n functions
Procedure n functionsProcedure n functions
Procedure n functions
 
Stored procedures
Stored proceduresStored procedures
Stored procedures
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql Procedure
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
 
Sql storeprocedure
Sql storeprocedureSql storeprocedure
Sql storeprocedure
 
05 Creating Stored Procedures
05 Creating Stored Procedures05 Creating Stored Procedures
05 Creating Stored Procedures
 

More from Prof.Nilesh Magar

More from Prof.Nilesh Magar (9)

Decision tree- System analysis and design
Decision tree- System analysis and designDecision tree- System analysis and design
Decision tree- System analysis and design
 
System concepts- System Analysis and design
System concepts- System Analysis and designSystem concepts- System Analysis and design
System concepts- System Analysis and design
 
Trigger in mysql
Trigger in mysqlTrigger in mysql
Trigger in mysql
 
Mysql creating stored function
Mysql  creating stored function Mysql  creating stored function
Mysql creating stored function
 
Crash recovery in database
Crash recovery in databaseCrash recovery in database
Crash recovery in database
 
Classification & preduction
Classification & preductionClassification & preduction
Classification & preduction
 
Frequent itemset mining methods
Frequent itemset mining methodsFrequent itemset mining methods
Frequent itemset mining methods
 
Feasibility study
Feasibility studyFeasibility study
Feasibility study
 
Data-ware Housing
Data-ware HousingData-ware Housing
Data-ware Housing
 

Recently uploaded

Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxFurkanTasci3
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Callshivangimorya083
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...Suhani Kapoor
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
Spark3's new memory model/management
Spark3's new memory model/managementSpark3's new memory model/management
Spark3's new memory model/managementakshesh doshi
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改atducpo
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 

Recently uploaded (20)

Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptx
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
Spark3's new memory model/management
Spark3's new memory model/managementSpark3's new memory model/management
Spark3's new memory model/management
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
 

Stored procedures

  • 2. STORED PROCEDURE  A procedure is a subroutine (like a subprogram) in a regular scripting language, stored in a database  In the case of MySQL, procedures are written in MySQL and stored in the MySQL database/server  A MySQL procedure has a name, a parameter list, and SQL statement(s)
  • 3. STORED PROCEDURES  Database program modules that are stored and executed by the DBMS at the server DELIMITER // CREATE PROCEDURE GetAllProducts() BEGIN SELECT * FROM products; END // DELIMITER ;
  • 4. WHY STORED PROCEDURES  Reduces Duplication of effort and improves software modularity  Multiple applications can use the stored procedure vs. the SQL statements being stored in the application language (PHP) Reduces communication and data transfer cost between client and server (in certain situations)  Instead of sending multiple lengthy SQL statements, the application only has to send the name and parameters of the Stored Procedure Can be more secure than SQL statements  Permission can be granted to certain stored procedures without granting access to database tables
  • 5. DISADVANTAGES OF STORED PROCEDURES  Difficult to debug  MySQL does not provide ways for debugging stored procedures Many stored procedures can increase memory use  The more stored procedures you use, the more memory is used Can be difficult to maintain and develop stored procedures  Another programming language to learn
  • 6. CREATING STORED PROCEDURES DELIMITER // CREATE PROCEDURE NAME BEGIN SQL STATEMENT END // DELIMITER ; DELIMITER // CREATE PROCEDURE GetAllProducts() BEGIN SELECT * FROM products; END // DELIMITER ;
  • 8.
  • 9. TYPES OF MYSQL PROCEDURES: 1. Procedure with no parameters: 2. Procedure with IN parameter: 3. Procedure with OUT parameter: 4. Procedure with IN-OUT parameter:
  • 10. 1. PROCEDURE WITH NO PARAMETERS: A procedure without parameters does not take any input or casts an output indirectly. It is simply called with its procedure name followed by () (without any parameters). It is used for simple queries.
  • 11. EXAMPLE PROCEDURE WITH NO PARAMETERS delimiter // create procedure display_book() -> begin -> select *from book; -> end // call display_book(); //
  • 12. PARAMETERS  There may be times where you want to pass information to the stored procedures  Getting user input from a form and using that input in a SQL statement
  • 13. PROCEDURE WITH IN PARAMETER:  An IN parameter is used to take a parameter as input such as an attribute  When we define an IN parameter in a procedure, the calling program has to pass an argument to the stored procedure  The value of an IN parameter is protected
  • 14. EXAMPLE PROCEDURE WITH IN PARAMETER: delimiter // create procedure update_price (IN temp_ISBN varchar(10), IN new_price integer) -> begin -> update book set price=new_price -> where ISBN=temp_ISBN; -> end; // call update_price(001, 600); //
  • 15. PROCEDURE WITH OUT PARAMETER:  An OUT parameter is used to pass a parameter as output or display like the select operator, but implicitly (through a set value)  The value of an OUT parameter can be changed inside the procedure and its new value is passed back to the calling program  A procedure cannot access the initial value of the OUT parameter when it starts.
  • 16. EXAMPLE ON PROCEDURE WITH OUT PARAMETER: delimiter // create procedure disp_max(OUT highestprice integer) -> begin -> select max(price) into highestprice from book; -> end; // call disp_max(@M); // select @M;
  • 17. PROCEDURE WITH IN-OUT PARAMETER:  An INOUT parameter is a combination of IN and OUT parameters  It means that the calling program may pass the argument, and the stored procedure can modify the INOUT parameter and pass the new value back to the calling program.
  • 18. EXAMPLE ON PROCEDURE WITH IN-OUT PARAMETER
  • 19. ARGUMENTS AND PARAMETERS DELIMITER // CREATE PROCEDURE GetOfficeByCountry(IN countryName VARCHAR(25 BEGIN SELECT * FROM offices WHERE country = countryName; END // DELIMITER ; Defining Calling CALL GetOfficeByCountry('USA') The values being copied from the calling stored procedure are calling arguments. The variables being copied into are called parameters.
  • 20. OUT PARAMETER DELIMITER // CREATE PROCEDURE CountOrderByStatus(IN orderStatus VARCHAR(25), OUT total INT) BEGIN SELECT count(orderNumber) INTO total FROM orders WHERE status = orderStatus; END// DELIMITER ; Defining Calling CALL CountOrderByStatus('Shipped',@total); SELECT @total; The out parameter is used outside of the stored procedure.
  • 21. VARIABLES  A variable is a name that refers to a value A name that represents a value stored in the computer memory C program int name=“Amit”; int age=25  MySQL DECLARE name VARCHAR(255) DECLARE age INT
  • 22. DELIMITER // CREATE PROCEDURE declare2(IN value int,IN name varchar(20)) BEGIN DECLARE x INT; DECLARE str VARCHAR(255); SET x = value; SET str = name; SELECT x,str; END// DELIMITER ;
  • 24. THE “IF” STATEMENT Mysql Syntax IF if_expression THEN commands [ELSEIF elseif_expression THEN commands] [ELSE commands] END IF; First line is known as the IF clause Includes the keyword IF followed by condition followed by the keyword THEN  When the IFstatement executes, the condition is tested, and if it is true the block statements are executed. Otherwise, block statements are skipped
  • 26. IF STATEMENT DELIMITER // CREATE PROCEDURE GetProductsInStockBasedOnQuantitityLevel(IN p_operator VARCHAR(255), IN p_quantityInStock INT) BEGIN IF p_operator = "<" THEN select * from products WHERE quantityInStock < p_quantityInStock; ELSEIF p_operator = ">" THEN select * from products WHERE quantityInStock > p_quantityInStock; END IF; END // DELIMITER ;
  • 27. IF STATEMENT  CREATE PROCEDURE GetProductsInStockBasedOnQuantitityLevel (IN p_operator VARCHAR(255), IN p_quantityInStock INT) The ooperator > or < The number in stock
  • 28. THE IF STATEMENT IF p_operator = "<" THEN select * from products WHERE quantityInStock < p_quantityInStock; ELSEIF p_operator = ">" THEN select * from products WHERE quantityInStock > p_quantityInStock; END IF;
  • 29. LOOPS  While  Repeat  Loop Repeats a set of commands until some condition is met Iteration: one execution of the body of a loop If a condition is never met, we will have an infinite loop
  • 30. WHILE LOOP WHILE expression DO Statements END WHILE The expression must evaluate to true or false while loop is known as a pretest loop Tests condition before performing an iteration Will never execute if condition is false to start with Requires performing some steps prior to the loop
  • 31. INFINITE LOOPS  Loops must contain within themselves a way to terminate  Something inside a while loop must eventually make the condition false  Infinite loop: loop that does not have a way of stopping  Repeats until program is interrupted  Occurs when programmer forgets to include stopping code in the loop
  • 32. WHILE LOOP DELIMITER // CREATE PROCEDURE WhileLoopProc() BEGIN DECLARE x INT; DECLARE str VARCHAR(255); SET x = 1; SET str = ''; WHILE x <= 5 DO SET str = CONCAT(str,x,','); SET x = x + 1; END WHILE; SELECT str; END// DELIMITER ;
  • 33. WHILE LOOP  Creating Variables DECLARE x INT; DECLARE str VARCHAR(255); SET x = 1; SET str = '';
  • 34. WHILE LOOP WHILE x <= 5 DO SET str = CONCAT(str,x,','); SET x = x + 1; END WHILE;