
MySQL create procedure example
Here is an example of creating a stored procedure in MySQL.
- 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.
-
Choose batabase using
use mydb;
statement if already exist or create new one usingcreate database mydb;
. -
Change delimiter from ; to //
delimiter //
- 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 //
- 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.
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.
Random Code Snippet Queries: Sql
- ERROR 3780 (HY000): Referencing column 'order_id' and referenced column 'id' in foreign key constraint 'order_items_ibfk_1' are incompatible.
- Get number of connections being used by a specific user in MySQL
- How to retrieve data from two tables with one SQL statement
- SQL query to delete records older than 6 months
- SQL query to delete all rows older than 30 days
- 1553 Cannot drop index 'posts_user_id_foreign': needed in a foreign key constraint
- How to get the list of all constraints in MySQL database
- How to create function in MySQL ?
- Get full name of a person in SQL using function
- Get comma separated ids from table using MySQL
- SQLSTATE[01000]: Warning: 1265 Data truncated for column 'visibility' at row 1
- Create Index in MySQL with example
- Change existing MySQL table column id to autoincrement
- How to create view in MySQL database
- Cannot drop index 'fk_role_id': needed in a foreign key constraint
- How to drop function in MySQL ?
- How to get yesterday's date in MySQL ?
- Get create table Query from existing table in phpMyAdmin
- Database connection using mySQLi with object-oriented
- How to show database tables in mysql using command line
- Check constraint in MySQL
- Drop foreign id column (user_id) in MySQL
- Difference between SQL and NoSQL databases
- How to get a list of MySQL views?
- Get data in random order with limit from table using mysql