Stored procedures are saved collections of one or more SQL statements for later use. They can be used for:
The three main advantages of stored procedures are:
Stored procedures also have disadvantages
For security, some DBMSs can allow only certain users to create stored procedures while permitting other users to execute them.
Creating a stored procedure:
-- Create a stored procedure to add a product to Products CREATE OR REPLACE PROCEDURE addProduct( p_id IN PRODUCTS.PROD_ID%TYPE, v_id IN PRODUCTS.VEND_ID%TYPE, p_name IN PRODUCTS.PROD_NAME%TYPE, p_price IN PRODUCTS.PROD_PRICE%TYPE, p_desc IN PRODUCTS.PROD_DESC%TYPE) IS BEGIN INSERT INTO Products("PROD_ID", "VEND_ID", "PROD_NAME", "PROD_PRICE", "PROD_DESC") VALUES (p_id, v_id, p_name, p_price, p_desc); COMMIT; END;
-- Use a stored procedure BEGIN addProduct('BR007','BRS01','A Waldorf Puppet',42.50, 'Made of wood and as a result, they are all different'); END;
-- Drop a stored procedure DROP PROCEDURE addProduct;
-- Create a stored procedure to add a product to Products DELIMITER // CREATE PROCEDURE addProduct( IN p_id char(10), IN v_id char(10), IN p_name char(255), IN p_price decimal(8,2), IN p_desc text ) BEGIN INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc) values (p_id, v_id, p_name, p_price, p_desc); END // DELIMITER ;
-- Use a stored procedure CALL addProduct('BR007','BRS01','A Waldorf Puppet',42.50, 'Made of wood and as a result, they are all different!');
-- Drop a stored procedure DROP PROCEDURE IF EXISTS addProduct;
Use a semicolon (;) to terminate a statement
The semicolon separates statements from each other
A stored procedure can contain several statements ending in semicolons
When defining a stored procedure, those statements must be treated as one definition
For this reason, temporarily choose a different delimiter when defining stored procedures in the MySQL client
-- Create a stored procedure to add a product to Products CREATE PROCEDURE addProduct @p_id char(10), @v_id char(10), @p_name char(255), @p_price decimal(8,2), @p_desc char(1000) AS BEGIN INSERT INTO Products([prod_id], [vend_id], [prod_name], [prod_price], [prod_desc]) VALUES(@p_id, @v_id, @p_name, @p_price, @p_desc) END GO
-- Use a stored procedure EXEC addProduct @p_id = 'BR007', @v_id = 'BRS01', @p_name = 'A Waldorf Puppet', @p_price = 42.50, @p_desc = 'Made of wood and as a result, they are all different';
-- Drop a stored procedure DROP PROCEDURE IF EXISTS addProduct;
SELECT * FROM Products ORDER BY prod_price;
| prod_id | vend_id | prod_name | prod_price | prod_desc |
| BNBG01 | DLL01 | Fish bean bag toy | 3.49 | Fish bean bag toy complete with bean bag worms with which to feed it |
| BNBG02 | DLL01 | Bird bean bag toy | 3.49 | Bird bean bag toy eggs are not included |
| BNBG03 | DLL01 | Rabbit bean bag toy | 3.49 | Rabbit bean bag toy comes with bean bag carrots |
| RGAN01 | DLL01 | Raggedy Ann | 4.99 | 18 inch Raggedy Ann doll |
| BR01 | BRS01 | 8 inch teddy bear | 5.99 | 8 inch teddy bear comes with cap and jacket |
| BR02 | BRS01 | 12 inch teddy bear | 8.99 | 12 inch teddy bear comes with cap and jacket |
| RYL01 | FNG01 | King doll | 9.49 | 12 inch king doll with royal garments and crown |
| RYL02 | FNG01 | Queen doll | 9.49 | 12 inch queen doll with royal garments and crown |
| BR03 | BRS01 | 18 inch teddy bear | 11.99 | 18 inch teddy bear comes with cap and jacket |
| BR007 | BRS01 | A Waldorf Puppet | 42.5 | Made of wood and as a result they are all different |