stored procedure in mysql code example

Example 1: MySQL Stored Procedures

DELIMITER $$

CREATE PROCEDURE GetCustomers()
BEGIN
	SELECT 
		customerName, 
		city, 
		state, 
		postalCode, 
		country
	FROM
		customers
	ORDER BY customerName;    
END $$

DELIMITER ;


-- Once you save the stored procedure, you can invoke it by using the CALL statement:

CALL GetCustomers();

Example 2: mysql create stored procedure

-- MySQL

-- example
DELIMITER $$ -- Changes delimiter to $$ so can use ; within the procedure
CREATE PROCEDURE select_employees()
BEGIN
	select * 
	from employees 
	limit 1000; -- Use the ; symbol within the procedure
END$$ 
DELIMITER ; -- Resets the delimiter

/* syntax:
DELIMITER $$ -- Changes delimiter to $$ so can use ; within the procedure
CREATE PROCEDURE <Your-procedure-name>(<argument1><argument2>...<argumentN>)
BEGIN
	<Code-that-stored-procedure-executes>; -- Use the ; symbol within the procedure
END$$
DELIMITER ; -- Resets the delimiter
*/

Example 3: procedure in sql

-Stored procedure is a group of SQL
statements that has been created 
and stored in the database.
-A stored procedure will accept input 
parameters so that a single procedure
can be used over the network by several
clients using different input data.
-A stored procedures will reduce
network traffic and increase the performance. 
If we modify a stored procedure all the
clients will get the updated stored procedure.

Sample of creating a stored procedure
CREATE PROCEDURE test_display AS
SELECT FirstName, LastName FROM tb_test
EXEC test_display

Example 4: create stored procedure mysql

DELIMITER $$

CREATE PROCEDURE GetAllProducts()
BEGIN
	SELECT *  FROM products;
END $$

DELIMITER ;


-- Once you save the stored procedure, you can invoke it by using the CALL statement:

CALL GetAllProducts();

Example 5: mysql stored procedure

Syntax:
-------------------
CREATE [DEFINER = { user | CURRENT_USER }]          
PROCEDURE sp_name ([proc_parameter[,...]])          
[characteristic ...] routine_body    
proc_parameter: [ IN | OUT | INOUT ] param_name type    
type:          
Any valid MySQL data type    
characteristic:          
COMMENT 'string'     
| LANGUAGE SQL      
| [NOT] DETERMINISTIC      
| { CONTAINS SQL | NO SQL | READS SQL DATA 
| MODIFIES SQL DATA }      
| SQL SECURITY { DEFINER | INVOKER }    
routine_body:      
Valid SQL routine statement

Example
mysql> DELIMITER $$ ;
mysql> CREATE PROCEDURE student_details()
> SELECT * FROM student_details; $$
Query OK, 0 rows affected (0.00 sec)

Tags:

Sql Example