A brief description of stored procedures

stored procedure
 Simply put, it is a collection of one or more MySQL statements that are saved for later use. It can be regarded as an approval document,
 Although their role is not limited to batch processing.
 A stored procedure is a collection of business logic and processes. You can create tables, update data, delete and so on in the stored procedure.
Why use stored procedures
 1. Simplify complex operations (as in the previous example) by encapsulating processing in easy-to-use units.
 2. This ensures the integrity of the data since it is not required to repeatedly establish a series of processing steps. If all developers and applications use the same (trial and test) stored procedure, the code used is the same. An extension of this is error prevention. The more steps that need to be performed, the greater the chance of error. Error prevention ensures data consistency.
 3. Simplify the management of changes. If the table name, column name or business logic (or whatever) changes, just change the stored procedure's
   code. People using it don't even need to know about the changes.
-------------------
a simple stored procedure
 create produce produceName()
 begin 
  select name from user;
 end;
Call the stored procedure:
 call produceName();
Delete the stored procedure:
 drop produce if exists produceName;
-----------------------
Stored procedure with parameters:
 create produce produceName(
  out min decimal(8,2),
  out avg decimal(8,2),
  out max decimal(8,2)
 )
 begin
  select MIN(price) into min from order;
  select AVG(price) into avg from order;
  select MAX(price) into max from order;
 END;
MySQL supports IN (passing to stored procedures), OUT (outgoing from stored procedures, as used here), and INOUT (passing in and out of stored procedures) types of parameters.
The code for the stored procedure is located within the BEGIN and END statements, which, as seen earlier, are a series of SELECT statements used to retrieve the values ​​and then save them to the corresponding variables (by specifying the INTO keyword) to call this modified stored procedure, it must be Specify 3 variable names: call produceName(@min, @avg, @max); then you can call and display the value of the variable:
select @min,@avg,@max;
--------------------------------
Use the in parameter, enter a user id, and return the total price of all orders for this user
create procedure getTotalById (
in userId int,
out total decimal(8,2)
)
BEGIN
  •  select SUM(r.price) from order r
  • where r.u_id = userId 
into total;
END;
 
call stored procedure
 call getTotalById(1,@total);
 select  @ total;
---------------------------------------------
 
For a more complicated process, obtain all the order prices of the user according to the user id, and dynamically select whether to add tax. The code is designed as follows
create procedure getTotalByUser2(
in userId int,
in falg boolean, --whether tax mark
out total decimal(8,2)
)
begin
DECLARE tmptotal DECIMAL(8,2);
DECLARE taxrate int DEFAULT 6;--? Default tax rate
select SUM(r.price) from order r 
where r.u_id = userId
into tmptotal; 
 
if falg then
select tmptotal + (tmptotal/1000*taxrate) into mptotal;
end if;
 
select tmptotal into total;
END;
The process passes in three parameters, user id, whether to add tax and the total price returned. Inside the process, two local variables tmptotal and taxrate are defined, and the result of the query is assigned to a temporary variable to judge whether to add tax. Finally, assign the value of the local variable to the output parameter.
call getTotalByUser2(1, false,  @ total); -- no tax
call getTotalByUser2(1, true, @total);  --加税
select  @ total;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326089268&siteId=291194637