MySQL supports Stored Procedures which can execute set of codes by applying relevant business logics. In this post we will see how to create a stored procedure and execute it.
Let us create the following tables
CREATE TABLE items(item_id INT, item_description VARCHAR(100));<br> CREATE TABLE sales(sales_id INT auto_increment KEY,item_id INT, sales_date DATETIME, sales_amount DECIMAL(12,2));<br> INSERT INTO items VALUES (1,'Television');<br> INSERT INTO items VALUES (2,'Mobile');<br> INSERT INTO items VALUES (3,'laptop');<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (1,'2014-01-01',1200);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (2,'2014-01-02',200);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (3,'2014-01-09',1700);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (3,'2014-01-29',1700);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (3,'2014-02-11',1700);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (1,'2014-02-16',1200);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (2,'2014-02-16',200);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (2,'2014-02-20',200);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (2,'2014-02-20',200);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (2,'2014-02-22',200);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (3,'2014-02-24',1700);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (1,'2014-02-24',1200);
Suppose you want to create a stored procedure which will accept item_description and returns the total sales_amount for theenireperiod, you can do it as shown below
DELIMITER $$<br> CREATE PROCEDURE Test.usp_get_sales<br> (<br> param_item_description VARCHAR(100)<br> )<br> BEGIN<br> SELECT item_description,SUM(sales_amount) AS sales_amount FROM items NATURAL<br> JOIN sales<br> WHERE item_description=param_item_description<br> GROUP BY item_description;<br> END;<br> $$<br> DELIMITER;<br>
Note that the creation of the stored procedure starts with setting the Delimiter $$. The default delimiter for MySQL statements are semicolon so in order to instruct the MySQL engine about the start and end of the stored procedure block, you need to use a different delimiter (which in this case $$ is used).
Now you can execute a stored procedure usingCALL keywordas shown below.
Execution 1
CALL usp_get_sales('Television');
When you execute the above code, the result is
Item_description sales_amountTelevision 3600.00
Execution 2
CALL usp_get_sales('laptop');
When you execute the above code, the result is
Item_description sales_amountlaptop 6800.00
Note:The parameters do not start with @ like we use in SQL Server. So in order todifferentiatebetween the actual column name and parameter name, the name param_item_description is used. You may need to use different naming conventions as you like.
I have previously written a similar article here: MySQL – How to Create Stored Procedure.
Reference:Pinal Dave (http://blog.sqlauthority.com)

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools

Atom editor mac version download
The most popular open source editor

SublimeText3 English version
Recommended: Win version, supports code prompts!
