close[x]


MySQL

MySQL-Home MySQL-Environment setup MySQL- Workbench MySQL-Basic syntax MySQL-Operator MySQL-Data type MySQL-Comments MySQL-Create DB MySQL-Drop DB MySQL-Select DB MySQL-Create Table MySQL-Drop table MySQL-Truncate MySQL-Primary Key MySQL-Foreign Key MySQL-Null MySQL-Increment MySQL-Having MySQL-Top MySQL-Insert Statement MySQL-Select Statement MySQL-Alter Statement MySQL-Where MySQL-And & Or MySQL-Default values MySQL-Exists MySQL-Order by MySQL-View MySQL-Update Statement MySQL-Delete Statement MySQL-Like MySQL-Sort MySQL-Limit MySQL-Min MySQL-Max MySQL-Group MySQL-In MySQL-Between MySQL-Union MySQL-Count MySQL-Average MySQL-Sum MySQL-Date & Time MySQL-Import MySQL-Export MySQL-Index MySQL-Temporary MySQL-Join MySQL-Full Join MySQL-Inner Join MySQL-Left Join MySQL-Right Join MySQL-Store Procedure MySQL-Injection MySQL-PHP connection



learncodehere.com





MySQL - Store Procedure

A procedure (often called a stored procedure) is a subroutine like a subprogram in a regular computing language, stored in database

A stored procedure is a prepared MySQL code that you can save, so the code can be reused over and over again.

So if you have an MySQL query that you write over and over again, save it as a stored procedure, and then just call it to execute it.

A procedure has a name, a parameter list, and SQL statement(s).

Stored procedures are fast.

Stored procedures are portable.


Syntax : Create Store Procedure


CREATE PROCEDURE procedure_name
[ (parameter datatype [, parameter datatype]) ]  
BEGIN  
    Declaration_section  
    Executable_section  
END;      

procedure_name: name of the procedure..

Parameter: number of parameters. It can be one or more than one.

declaration_section: all variables are declared.

executable_section: code for the function is written here.


Let's put these statements into real use.

We've a table named Students in our database that contains the following records:

ROLL_NO NAME SUBJECT
1 Will C++
2 SAM Python
3 Sara HTML
4 Rim Java
5 Micheal SQL
6 Lara


Example : Create Procedure


DELIMITER $$  
CREATE PROCEDURE get_students()  
BEGIN  
SELECT * FROM Students where ROLL_NO > 2;  
END$$   

The above Store Procedure runs when we call it. Because store procedure functions are stored in the database.


Example : Call Store Procedure


                    CALL get_students();
            

Now, after executing the above MySQL call statement,

ROLL_NO NAME SUBJECT
3 Sara HTML
4 Rim Java
5 Micheal SQL
6 Lara


You can also pass parameters to a stored procedure, so that the stored procedure can act based on the parameter value(s) that is passed.


Example : Create Procedure with Parameter


DELIMITER $$  
CREATE PROCEDURE get_students( @ROLL_NO int)  
BEGIN  
SELECT * FROM Students where ROLL_NO > @ROLL_NO;  
END$$   

The above Store Procedure runs when we call it. Because store procedure functions are stored in the database.

When we call the store procedure we have to pass a parameter.


Example : Call Store Procedure


 CALL get_students(2);
         

Now, after executing the above MySQL call statement,

ROLL_NO NAME SUBJECT
3 Sara HTML
4 Rim Java
5 Micheal SQL
6 Lara