Stored Procedures In simple terms, is a collection for later use and saved one or more MySQL statement.
Think of it as a batch file, although their role is not limited to the batch.
Stored procedures advantages:
1. processing unit packaged in easy to use, simplifying the complex operation.
2. does not require repeated establish a series of processing steps, which ensures data integrity. Team development process, all developers and applications use the same (tried and tested) stored procedure code is used are the same, but also may avoid erroneous results.
3. simplify the management of change. If the table name, column name or business logic (or something else) there is a change, just change the code stored procedures.
4. improve performance. Using stored procedures is faster than using a single SQL statement.
5. There are some can only be used in a single request and the elements characteristic MySQL, stored procedures can use to write code more powerful and more flexible
brief summary: simple, safe and high performance.
1. How to execute a stored procedure:
use the Call keyword, CALL accept the name of the stored procedure and the need for any of the parameters passed to it.
CALL productPricing(@avgPrice, @avgSum);
as follows:
Note:
1. The first call should follow - execution (12>) of the order
2. If you have invoked once, but changed course to create a stored procedure sql execute statement, then you need to perform 3 Delete already created stored procedures, and then executed sequentially 1, 2, otherwise the result will not be updated even newspaper 'pROCEDURE productPricing already exists' error.
2. Create and execute a stored procedure with parameters
IN (to the stored procedure), OUT (outgoing from the stored procedure) and INOUT (stored process incoming and outgoing) the type of parameter.
CREATE PROCEDURE productInfo(
OUT p1 float(8, 2),
OUT p2 DECIMAL(8, 2)
)
BEGIN
SELECT help_topic_id into p1 from help_topic where name = 'CONTAINS';
SELECT help_topic_id into p2 from help_topic where name = 'HEX';
END;
CALL productInfo(@p1, @p2);
SELECT @p1 as p1, @p2 as p2;
Results of the:
3. Establish and implement intelligent storage process
CREATE PROCEDURE productInfo(
IN p1 float(8, 2),
OUT p2 DECIMAL(8, 2)
)
BEGIN
if p1 = 'CONTAINS' then
SELECT help_topic_id into p2 from help_topic where name = 'CONTAINS';
else
SELECT help_topic_id into p2 from help_topic where name = 'HEX';
END IF;
END;
CALL productInfo('CONTAINS', @p2);
SELECT @p2 as p2;
Results of the: