Stored procedures are blocks of SQL code that can be stored on a database server for repeated execution. They allow for encapsulation of repetitive tasks, improved security, and better performance compared to executing SQL statements directly from a client application. Key features of stored procedures include portability, reusability, and the ability to accept parameters and return values. Common SQL statements like SELECT, INSERT, UPDATE and DELETE can be used within a stored procedure. Functions are similar but can only return a single value and have additional limitations compared to procedures.
The document discusses several SQL concepts:
[1] Subqueries, which can be noncorrelated (independent of the outer query) or correlated (contains references to the outer query). Correlated subqueries cannot be run independently.
[2] Views, which provide security, simplify queries, and insulate from changes, but can reduce performance and manageability. Views restrict access and update capabilities.
[3] Stored procedures, which increase performance but require specialized skills. Stored procedures accept parameters, contain multiple statements, and perform modifications, while views are limited to single SELECTs.
This document discusses stored procedures in SQL. It defines a stored procedure as a pre-compiled SQL statement that is saved in the database and can be run from the database. Stored procedures do not execute automatically. They improve performance by reducing network traffic and allowing code reuse. The document provides the syntax for creating stored procedures, including the use of parameters, and calling stored procedures. It gives an example stored procedure that retrieves all products from a table.
This document discusses stored procedures in MySQL and MSSQL, including their advantages, syntax, and examples. It also covers the differences between procedures and functions, and provides an example of creating a trigger to update total department salaries when employees are inserted, updated, or deleted.
In this presentation, I have covered an important topic which is Stored Procedure. This presentation will give a basic understanding of what a stored procedure is? and then will guide you on how to create and drop a stored procedure in Mysql.
This document provides an introduction to stored procedures in MySQL. It begins with a definition of stored procedures as subprograms stored in a database that can contain SQL statements. The document then discusses why stored procedures are useful, including that they are fast, portable, reusable components that centralize logic in the database. It also covers how to set up MySQL for stored procedures and demonstrates starting the mysql client.
This document provides an overview of stored procedures in MySQL, including what they are, why they are used, how they work, and examples of different types of stored procedures. Key points covered include:
- Stored procedures are subroutines that consolidate and centralize database logic. They can improve performance by reducing network traffic and allowing code reuse.
- Examples demonstrate basic stored procedures without parameters, as well as those using parameters, IF/THEN statements, CASE statements, LOOPs, and CURSORs to iterate through result sets.
- Limitations include increased memory usage, difficulty debugging, and specialized skill requirements for development and maintenance.
This document discusses stored procedures in SQL Server. It covers creating, updating, and deleting stored procedures, as well as using parameters, variables, and error handling within stored procedures. Several key benefits of stored procedures are that they reduce network traffic, can be optimized by the database compiler, and allow centralized management of logic and security. The document also provides examples of creating parameterized and non-parameterized stored procedures.
The document provides an introduction to stored procedures in SQL. Key points include:
- Stored procedures allow code to be executed faster than batches by pre-compiling the code.
- They centralize business logic and error handling routines for consistent implementation across users.
- Parameters can be passed into stored procedures to make them more flexible. Output parameters allow returning values.
- Best practices include adding comments, error handling, and using transactions for consistency across nested stored procedures.
The document provides an introduction to stored procedures in SQL. Key points include:
- Stored procedures allow code to be executed as a batch after being compiled once, improving performance over executing individual SQL statements.
- Stored procedures can accept input parameters, return output parameters, and be used to enforce consistent implementation of business logic and error handling.
- Best practices for stored procedures include adding documentation, error handling, and using input/output parameters to make procedures more flexible and reusable.
A stored procedure is a subroutine available to applications that access a relational database. Extensive or complex SQL processing is moved into stored procedures to improve performance. Stored procedures offer advantages like improved security, reduced network traffic, and easier maintenance compared to embedding SQL statements in an application. They allow parameters to be passed in and support flow control structures like IF/THEN clauses.
A stored procedure is a subroutine stored in a database that can be called to perform a specific task. Stored procedures consolidate and centralize logic that was originally implemented across multiple applications. They improve performance by only sending the procedure name and parameters rather than multiple SQL statements. Stored procedures also increase security by allowing access permissions to be set at the procedure level. Common uses include data validation and access control mechanisms integrated into the database.
The document provides information about stored procedures in databases:
- A stored procedure is a way to encapsulate repetitive tasks like queries into reusable code blocks stored in the database.
- Stored procedures offer advantages like precompiled execution for improved performance, reduced network traffic, code reuse, and enhanced security.
- The example shows how to create a stored procedure using delimiters to change parsing behavior and pass parameters to a procedure. Cursors allow fetching multiple rows from a result set into variables.
This document discusses stored procedures in databases. It defines a stored procedure as a program code stored in the database server that encapsulates repetitive tasks. Stored procedures can make tasks faster by pre-compiling the code rather than compiling it each time. The document provides the syntax for creating, calling, and deleting stored procedures. It also discusses parameters, cursors, and declare handlers. Examples are given to illustrate stored procedures.
1. The document discusses various SQL concepts including DCL (Data Control Language), DDL (Data Definition Language), DML (Data Manipulation Language), functions, users, phpMyAdmin, and procedures.
2. Key SQL commands covered include COMMIT, ROLLBACK, GRANT, REVOKE, CREATE, ALTER, DROP, SELECT, INSERT, UPDATE, DELETE.
3. The document also discusses creating, using, and dropping MySQL users, as well as the features and uses of phpMyAdmin for database administration.
This document discusses stored procedures in SQL Server. It begins by explaining that stored procedures allow encapsulation of repetitive tasks and are stored in the database data dictionary. It then shows how stored procedures reduce network traffic and client-server communication compared to individual SQL statements. The document provides examples of how to create a stored procedure using CREATE PROCEDURE and how to call it using EXEC. It notes advantages like precompiled execution, reduced traffic, code reuse, and security control. It also demonstrates using parameters, loops, conditions and variables inside stored procedures.
The document provides information on various SQL commands used for data manipulation and control, database objects like tables and schemas, users and privileges in SQL. It discusses commands like COMMIT, ROLLBACK, GRANT, REVOKE used for transactions and managing privileges. It also summarizes creation, alteration and deletion of database objects using commands like CREATE, ALTER, DROP and functions like SELECT, INSERT, UPDATE, DELETE for data manipulation. The document further discusses concepts like stored procedures in SQL and use of phpMyAdmin for administering MySQL databases.
This document provides an introduction and overview of stored procedures and functions in SQL. It discusses transaction management using COMMIT and ROLLBACK statements. It defines stored procedures as precompiled collections of SQL statements that can accept parameters and return values. Stored procedures offer benefits like modular programming and faster execution. The document also introduces user-defined functions and provides examples of creating and executing stored procedures and functions.
Stored Procedure in SQL Server can be defined as the set of logical group of SQL statements which are grouped to perform a specific task. There are many benefits of using a stored procedure. The main benefit of using a stored procedure is that it increases the performance of the database.The other benefits of using the Stored Procedure are given below.
This document discusses stored procedures in MySQL. Key points:
- Stored procedures are subroutines stored in a database that can take parameters and return values. They are written in MySQL and stored on the server.
- Benefits include reducing duplication, improving security, and reducing network traffic compared to sending multiple SQL statements.
- Disadvantages include difficulty debugging and increased memory usage with many procedures.
- Procedures can take IN, OUT, and INOUT parameters to pass values between the calling code and procedure. Variables and conditionals like IF statements can also be used inside procedures.
This document discusses stored procedures in SQL Server. It defines stored procedures as subroutines that are stored in a database's data dictionary and can be used to perform repetitive tasks. The document provides steps for creating a stored procedure in SQL Server Management Studio, including specifying a name, parameters, and body. It also lists some advantages of stored procedures like precompiled execution for improved performance when called repeatedly and more secure control over user permissions.
This document presents the design and implementation of MySQL/JVM, a framework for embedding the Java Virtual Machine (JVM) runtime environment into the MySQL database server. This allows stored procedures and functions in MySQL to be written in the Java programming language, leveraging Java's robust libraries. Currently, MySQL only supports a basic procedural language for stored procedures. MySQL/JVM aims to address this limitation and enable functionality like XML validation and encryption that are difficult to implement in MySQL's native language.
Sql server ___________session_18(stored procedures)Ehtisham Ali
Stored procedures allow users to save and reuse SQL code. There are three types of stored procedures: user defined, extended, and system stored procedures. User defined stored procedures are custom procedures that perform repetitive tasks. Extended stored procedures allow SQL to interact with the operating system. System stored procedures are used to interact with system tables and are prefixed with "sp_". Stored procedures can take parameters, return results, and include logic like IF/ELSE statements. They improve code reuse and simplify tasks like accessing data across different queries or applications.
The document discusses stored procedures and embedded SQL. Stored procedures allow functions to be stored on the database server and reduce network traffic by being invoked multiple times. They can be written in languages like SQL, C, or Java. Embedded SQL is used to interface SQL with a host programming language by embedding SQL statements directly into program code using syntax like EXEC SQL.
A stored procedure is nothing more than prepared SQL code that you save so you can reuse the code over and over again. So if you think about a query that you write over and over again, instead of having to write that query each time you would save it as a stored procedure and then just call the stored procedure to execute the SQL code that you saved as part of the stored procedure
This document provides an overview and introduction to MySQL stored procedures. It discusses what stored procedures are, why they are used, and provides a tutorial on how to create stored procedures, functions, and triggers in MySQL. It covers stored programming fundamentals such as language basics, blocks, conditional statements, iterative programming, and using SQL within stored programs. The document is intended to be a practical guide for learning MySQL stored procedure programming.
The document discusses stored procedures and triggers in databases, noting that stored procedures are reusable SQL code stored on the database that can increase performance, while triggers automatically run SQL code in response to changes made to a database table, such as inserts, updates or deletes. Both stored procedures and triggers can help with tasks like validation, auditing, and increasing performance by reducing traffic between applications and databases.
The document provides an introduction to stored procedures in SQL. Key points include:
- Stored procedures allow code to be executed faster than batches by pre-compiling the code.
- They centralize business logic and error handling routines for consistent implementation across users.
- Parameters can be passed into stored procedures to make them more flexible. Output parameters allow returning values.
- Best practices include adding comments, error handling, and using transactions for consistency across nested stored procedures.
The document provides an introduction to stored procedures in SQL. Key points include:
- Stored procedures allow code to be executed as a batch after being compiled once, improving performance over executing individual SQL statements.
- Stored procedures can accept input parameters, return output parameters, and be used to enforce consistent implementation of business logic and error handling.
- Best practices for stored procedures include adding documentation, error handling, and using input/output parameters to make procedures more flexible and reusable.
A stored procedure is a subroutine available to applications that access a relational database. Extensive or complex SQL processing is moved into stored procedures to improve performance. Stored procedures offer advantages like improved security, reduced network traffic, and easier maintenance compared to embedding SQL statements in an application. They allow parameters to be passed in and support flow control structures like IF/THEN clauses.
A stored procedure is a subroutine stored in a database that can be called to perform a specific task. Stored procedures consolidate and centralize logic that was originally implemented across multiple applications. They improve performance by only sending the procedure name and parameters rather than multiple SQL statements. Stored procedures also increase security by allowing access permissions to be set at the procedure level. Common uses include data validation and access control mechanisms integrated into the database.
The document provides information about stored procedures in databases:
- A stored procedure is a way to encapsulate repetitive tasks like queries into reusable code blocks stored in the database.
- Stored procedures offer advantages like precompiled execution for improved performance, reduced network traffic, code reuse, and enhanced security.
- The example shows how to create a stored procedure using delimiters to change parsing behavior and pass parameters to a procedure. Cursors allow fetching multiple rows from a result set into variables.
This document discusses stored procedures in databases. It defines a stored procedure as a program code stored in the database server that encapsulates repetitive tasks. Stored procedures can make tasks faster by pre-compiling the code rather than compiling it each time. The document provides the syntax for creating, calling, and deleting stored procedures. It also discusses parameters, cursors, and declare handlers. Examples are given to illustrate stored procedures.
1. The document discusses various SQL concepts including DCL (Data Control Language), DDL (Data Definition Language), DML (Data Manipulation Language), functions, users, phpMyAdmin, and procedures.
2. Key SQL commands covered include COMMIT, ROLLBACK, GRANT, REVOKE, CREATE, ALTER, DROP, SELECT, INSERT, UPDATE, DELETE.
3. The document also discusses creating, using, and dropping MySQL users, as well as the features and uses of phpMyAdmin for database administration.
This document discusses stored procedures in SQL Server. It begins by explaining that stored procedures allow encapsulation of repetitive tasks and are stored in the database data dictionary. It then shows how stored procedures reduce network traffic and client-server communication compared to individual SQL statements. The document provides examples of how to create a stored procedure using CREATE PROCEDURE and how to call it using EXEC. It notes advantages like precompiled execution, reduced traffic, code reuse, and security control. It also demonstrates using parameters, loops, conditions and variables inside stored procedures.
The document provides information on various SQL commands used for data manipulation and control, database objects like tables and schemas, users and privileges in SQL. It discusses commands like COMMIT, ROLLBACK, GRANT, REVOKE used for transactions and managing privileges. It also summarizes creation, alteration and deletion of database objects using commands like CREATE, ALTER, DROP and functions like SELECT, INSERT, UPDATE, DELETE for data manipulation. The document further discusses concepts like stored procedures in SQL and use of phpMyAdmin for administering MySQL databases.
This document provides an introduction and overview of stored procedures and functions in SQL. It discusses transaction management using COMMIT and ROLLBACK statements. It defines stored procedures as precompiled collections of SQL statements that can accept parameters and return values. Stored procedures offer benefits like modular programming and faster execution. The document also introduces user-defined functions and provides examples of creating and executing stored procedures and functions.
Stored Procedure in SQL Server can be defined as the set of logical group of SQL statements which are grouped to perform a specific task. There are many benefits of using a stored procedure. The main benefit of using a stored procedure is that it increases the performance of the database.The other benefits of using the Stored Procedure are given below.
This document discusses stored procedures in MySQL. Key points:
- Stored procedures are subroutines stored in a database that can take parameters and return values. They are written in MySQL and stored on the server.
- Benefits include reducing duplication, improving security, and reducing network traffic compared to sending multiple SQL statements.
- Disadvantages include difficulty debugging and increased memory usage with many procedures.
- Procedures can take IN, OUT, and INOUT parameters to pass values between the calling code and procedure. Variables and conditionals like IF statements can also be used inside procedures.
This document discusses stored procedures in SQL Server. It defines stored procedures as subroutines that are stored in a database's data dictionary and can be used to perform repetitive tasks. The document provides steps for creating a stored procedure in SQL Server Management Studio, including specifying a name, parameters, and body. It also lists some advantages of stored procedures like precompiled execution for improved performance when called repeatedly and more secure control over user permissions.
This document presents the design and implementation of MySQL/JVM, a framework for embedding the Java Virtual Machine (JVM) runtime environment into the MySQL database server. This allows stored procedures and functions in MySQL to be written in the Java programming language, leveraging Java's robust libraries. Currently, MySQL only supports a basic procedural language for stored procedures. MySQL/JVM aims to address this limitation and enable functionality like XML validation and encryption that are difficult to implement in MySQL's native language.
Sql server ___________session_18(stored procedures)Ehtisham Ali
Stored procedures allow users to save and reuse SQL code. There are three types of stored procedures: user defined, extended, and system stored procedures. User defined stored procedures are custom procedures that perform repetitive tasks. Extended stored procedures allow SQL to interact with the operating system. System stored procedures are used to interact with system tables and are prefixed with "sp_". Stored procedures can take parameters, return results, and include logic like IF/ELSE statements. They improve code reuse and simplify tasks like accessing data across different queries or applications.
The document discusses stored procedures and embedded SQL. Stored procedures allow functions to be stored on the database server and reduce network traffic by being invoked multiple times. They can be written in languages like SQL, C, or Java. Embedded SQL is used to interface SQL with a host programming language by embedding SQL statements directly into program code using syntax like EXEC SQL.
A stored procedure is nothing more than prepared SQL code that you save so you can reuse the code over and over again. So if you think about a query that you write over and over again, instead of having to write that query each time you would save it as a stored procedure and then just call the stored procedure to execute the SQL code that you saved as part of the stored procedure
This document provides an overview and introduction to MySQL stored procedures. It discusses what stored procedures are, why they are used, and provides a tutorial on how to create stored procedures, functions, and triggers in MySQL. It covers stored programming fundamentals such as language basics, blocks, conditional statements, iterative programming, and using SQL within stored programs. The document is intended to be a practical guide for learning MySQL stored procedure programming.
The document discusses stored procedures and triggers in databases, noting that stored procedures are reusable SQL code stored on the database that can increase performance, while triggers automatically run SQL code in response to changes made to a database table, such as inserts, updates or deletes. Both stored procedures and triggers can help with tasks like validation, auditing, and increasing performance by reducing traffic between applications and databases.
This presentation has been made keeping in mind the students of undergraduate and postgraduate level. To keep the facts in a natural form and to display the material in more detail, the help of various books, websites and online medium has been taken. Whatever medium the material or facts have been taken from, an attempt has been made by the presenter to give their reference at the end.
The Lohar dynasty of Kashmir is a new chapter in the history of ancient India. We get to see an ancient example of a woman ruling a dynasty in the Lohar dynasty.
Dastur_ul_Amal under Jahangir Key Features.pptxomorfaruqkazi
Dastur_ul_Amal under Jahangir Key Features
The Dastur-ul-Amal (or Dasturu’l Amal) of Emperor Jahangir is a key administrative document from the Mughal period, particularly relevant during Jahangir’s reign (1605–1627). The term "Dastur-ul-Amal" broadly translates to "manual of procedures" or "regulations for administration", and in Jahangir’s context, it refers to his set of governance principles, administrative norms, and regulations for court officials and provincial administration.
Classification of mental disorder in 5th semester bsc. nursing and also used ...parmarjuli1412
Classification of mental disorder in 5th semester Bsc. Nursing and also used in 2nd year GNM Nursing Included topic is ICD-11, DSM-5, INDIAN CLASSIFICATION, Geriatric-psychiatry, review of personality development, different types of theory, defense mechanism, etiology and bio-psycho-social factors, ethics and responsibility, responsibility of mental health nurse, practice standard for MHN, CONCEPTUAL MODEL and role of nurse, preventive psychiatric and rehabilitation, Psychiatric rehabilitation,
How to Configure Extra Steps During Checkout in Odoo 18 WebsiteCeline George
In this slide, we’ll discuss on how to Configure Extra Steps During Checkout in Odoo 18 Website. Odoo website builder offers a flexible way to customize the checkout process.
As of 5/17/25, the Southwestern outbreak has 865 cases, including confirmed and pending cases across Texas, New Mexico, Oklahoma, and Kansas. Experts warn this is likely a severe undercount. The situation remains fluid, though we are starting to see a significant reduction in new cases in Texas. Experts project the outbreak could last up to a year.
CURRENT CASE COUNT: 865 (As of 5/17/2025)
- Texas: 720 (+2) (62% of cases are in Gaines County)
- New Mexico: 74 (+3) (92.4% of cases are from Lea County)
- Oklahoma: 17
- Kansas: 54 (38.89% of the cases are from Gray County)
HOSPITALIZATIONS: 102
- Texas: 93 - This accounts for 13% of all cases in Texas.
- New Mexico: 7 – This accounts for 9.47% of all cases in New Mexico.
- Kansas: 2 - This accounts for 3.7% of all cases in Kansas.
DEATHS: 3
- Texas: 2 – This is 0.28% of all cases
- New Mexico: 1 – This is 1.35% of all cases
US NATIONAL CASE COUNT: 1,038 (Confirmed and suspected)
INTERNATIONAL SPREAD (As of 5/17/2025)
Mexico: 1,412 (+192)
- Chihuahua, Mexico: 1,363 (+171) cases, 1 fatality, 3 hospitalizations
Canada: 2,191 (+231) (Includes
Ontario’s outbreak, which began in November 2024)
- Ontario, Canada – 1,622 (+182), 101 (+18) hospitalizations
How to Use Upgrade Code Command in Odoo 18Celine George
In this slide, we’ll discuss on how to use upgrade code Command in Odoo 18. Odoo 18 introduced a new command-line tool, upgrade_code, designed to streamline the migration process from older Odoo versions. One of its primary functions is to automatically replace deprecated tree views with the newer list views.
How to Manage Cross Selling in Odoo 18 SalesCeline George
In this slide, we’ll discuss on how to Manage cross selling in Odoo 18 Sales. Cross-selling is a powerful sales technique that involves recommending complementary or related products to a customer who is already considering a purchase.
GUESS WHO'S HERE TO ENTERTAIN YOU DURING THE INNINGS BREAK OF IPL.
THE QUIZ CLUB OF PSGCAS BRINGS YOU A QUESTION SUPER OVER TO TRIUMPH OVER IPL TRIVIA.
GET BOWLED OR HIT YOUR MAXIMUM!
As of 5/14/25, the Southwestern outbreak has 860 cases, including confirmed and pending cases across Texas, New Mexico, Oklahoma, and Kansas. Experts warn this is likely a severe undercount. The situation remains fluid, with case numbers expected to rise. Experts project the outbreak could last up to a year.
CURRENT CASE COUNT: 860 (As of 5/14/2025)
Texas: 718 (+6) (62% of cases are in Gaines County)
New Mexico: 71 (92.4% of cases are from Lea County)
Oklahoma: 17
Kansas: 54 (+6) (38.89% of the cases are from Gray County)
HOSPITALIZATIONS: 102 (+2)
Texas: 93 (+1) - This accounts for 13% of all cases in Texas.
New Mexico: 7 – This accounts for 9.86% of all cases in New Mexico.
Kansas: 2 (+1) - This accounts for 3.7% of all cases in Kansas.
DEATHS: 3
Texas: 2 – This is 0.28% of all cases
New Mexico: 1 – This is 1.41% of all cases
US NATIONAL CASE COUNT: 1,033 (Confirmed and suspected)
INTERNATIONAL SPREAD (As of 5/14/2025)
Mexico: 1,220 (+155)
Chihuahua, Mexico: 1,192 (+151) cases, 1 fatality
Canada: 1,960 (+93) (Includes Ontario’s outbreak, which began November 2024)
Ontario, Canada – 1,440 cases, 101 hospitalizations
2. Introduction to MySQL CREATE PROCEDURE statement
The stored procedure is SQL statements wrapped within
the CREATE PROCEDURE statement.
To create a stored procedure, you use the CREATE PROCEDURE
statement.
Basic syntax of the CREATE PROCEDURE statement:
CREATE PROCEDURE sp_name(parameter_list)
BEGIN
statements;
END;
3. In this syntax:
• First, define the name of the stored procedure sp_name after the
CREATE PROCEDURE keywords.
• Second, specify the parameter list (parameter_list) inside the
parentheses followed by the stored procedure’s name. If the stored
procedure has no parameters, you can use an empty parentheses ().
• Third, write the stored procedure body that consists of one or more
valid SQL statements between the BEGIN and END block.
4. If you attempt to create a stored procedure that already exists, MySQL
will issue an error.
To prevent the error, you can add an additional clause IF NOT EXISTS
after the CREATE PROCEDURE keywords:
CREATE PROCEDURE [IF NOT EXISTS] sp_name
([parameter[,...]]) routine_body;
5. The following SELECT statement returns all rows in the table customers
from the sample database:
SELECT
customerName,
city,
state,
postalCode,
country
FROM
customers
ORDER BY customerName;
7. If you intend to save this query on the database server for later
execution, one way to achieve this is by using a stored procedure.
The following CREATE PROCEDURE statement creates a new stored
procedure encapsulating the query above:
8. DELIMITER $$
CREATE PROCEDURE GetCustomers()
BEGIN
SELECT
customerName,
city,
state,
postalCode,
country
FROM
customers
ORDER BY customerName;
END$$
DELIMITER ;
9. A stored procedure is a set of declarative SQL statements stored within
the MySQL Server. In this example, we have just created a stored
procedure named GetCustomers().
After saving the stored procedure, you can invoke it by using the CALL
statement:
CALL GetCustomers();
10. The statement returns the same result as the query.
The initial invocation of a stored procedure involves the following
actions by MySQL:
• First, find the stored procedure by its name in the database catalog.
• Second, compile the code of the stored procedure.
• Third, store the compiled stored procedure in a cache memory area.
• Finally, execute the stored procedure.
11. MySQL Stored Procedures advantages
• Reduce network traffic – Stored procedures help reduce the network
traffic between applications and MySQL servers. Instead of sending
multiple lengthy SQL statements, applications only need to send the name
and parameters of the stored procedures.
• Centralize business logic in the database – You can use stored procedures
to implement reusable business logic across multiple applications. They
streamline the process, reducing the need to duplicate the same logic in
multiple applications and contributing a more consistent database.
• Make the database more secure – You can grant specific privileges to
applications, allowing access to particular stored procedures without
providing any privileges to the underlying tables.
12. MySQL stored procedures
disadvantages
• Resource usage – If you use many stored procedures, the memory usage of
every connection will significantly increase. Additionally, an excessive use of
logical operations in the stored procedures can lead to increased CPU usage, as
MySQL is not well-designed for such operations.
• Troubleshooting – Debugging stored procedures is quite challenging.
Unfortunately, MySQL lacks facilities for debugging stored procedures, a
feature available in other enterprise database products such as Oracle
Database and SQL Server.
• Maintenances – Developing and maintaining stored procedures often demands
a specialized skill set not universally possessed by all application developers,
potentially causing issues in both application development and maintenance.
13. Simple stored procedure
DELIMITER //
CREATE PROCEDURE GetEmployeeCount()
BEGIN
SELECT COUNT(*) AS employee_count FROM employees;
END//
DELIMITER ;
This stored procedure simply returns the count of employees from the employees
table.
14. Stored Procedure with Parameters
DELIMITER //
CREATE PROCEDURE GetEmployeeByDepartment(IN dept_id INT)
BEGIN
SELECT * FROM employees WHERE department_id = dept_id;
END//
DELIMITER ;
This stored procedure takes a department ID as input parameter and returns all
employees belonging to that department.
15. Stored Procedure with Output
Parameters
DELIMITER //
CREATE PROCEDURE GetDepartmentCount(OUT dept_count INT)
BEGIN
SELECT COUNT(*) INTO dept_count FROM departments;
END//
DELIMITER ;
This stored procedure calculates the count of departments and returns it via an output
parameter.
16. Stored Procedure with Conditional
Logic:
DELIMITER //
CREATE PROCEDURE GetHighSalaryEmployees()
BEGIN
DECLARE min_salary DECIMAL(10, 2);
SET min_salary = 50000;
SELECT * FROM employees WHERE salary >= min_salary;
END//
DELIMITER ;
This stored procedure retrieves employees with salaries equal to or higher than a specified
threshold.
17. To call these stored procedures, you can use the CALL statement
followed by the procedure name and any necessary parameters.
For example:
CALL GetEmployeeCount();
18. How to execute stored procedure
1. Open MySQL Workbench
2. Create New tab to run SQL statements.
3. Enter the SQL statements for stored procedure in your new tab.
19. 4. Execute the store procedure statements by clicking the ‘lightning’
icon shown below. That will call stored procedure in MySQL
Workbench.
5. Expand the stored procedure node in right pane. You
should see the name of your stored procedure (get_products).
If you don’t see it, then hit refresh button next to SCHEMAS
20. Drop a Stored Procedure
You can use the MySQL
workbench wizard to drop
the procedure. To drop any
procedure, expand sakila
schema Expand Stored
Procedures Right-click on
sp_GetMovies Click on
Drop Stored Procedure.
21. To drop the stored procedure, you can use the drop procedure command. The syntax is
following
Drop procedure [IF EXISTS] <Procedure Name>
In the syntax, the name of the stored procedure must be followed by the Drop Procedure
keyword. If you want to drop the sp_getCustomers procedure from the sakila database, you can
run the following query.
Drop procedure sp_getCustomers
When you try to drop the procedure that does not exist on a database, the query shows an error:
ERROR 1305 (42000): PROCEDURE sakila.getCustomer does not exist
To avoid this, you can include the [IF EXISTS] option in the drop procedure command. When you
include the IF EXISTS keyword, instead of an error, the query returns a warning:
24. Summary
• A stored procedure is a wrapper of a set of SQL statements stored in the
MySQL database server.
• The advantages of stored procedures include reduced network traffic,
enhanced code reusability, improved security through controlled access,
streamlined implementation of business logic, and the ability to grant
specific privileges to applications without exposing underlying database
structures.
• The disadvantages of stored procedures include increased memory usage
for each connection, challenges in debugging due to a lack of dedicated
tools, and the necessity for a specialized skill set, which not all application
developers may possess, leading to potential difficulties in both
development and maintenance processes.