MySQL create procedure example

Created at 29-Apr-2023 , By samar

Here is an example of creating a stored procedure in MySQL.

  1. First connect to MysQL server using below command.
mysql -u root -p 

The command mysql -u root -p is used to connect to a MySQL database server as the root user from the command line interface.

  1. Choose batabase using use mydb; statement if already exist or create new one using create database mydb;.

  2. Change delimiter from ; to //

delimiter //
  1. Create procedure using below sql statement.
create procedure calculate_tax(in subtotal decimal(10,2), out total decimal(10,2), inout tax_rate decimal(10,2) ) begin declare tax decimal(10,2); set tax = subtotal * (tax_rate / 100) ; set total = subtotal + tax ; if subtotal > 1000 then set tax_rate = 10; end if ; return  total, tax_rate; end //
  1. Call the procedure
 call calculate_tax(1000, @total, @tax_rate);

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.