Stored procedure in Mysql

1. Introduction to stored procedures (exam-specific crap)

A stored procedure is a set of SQL statements to complete a specific function, compiled and stored in the database, and the user can execute it with a given stored procedure name and parameters (if the stored procedure has parameters).

① Stored procedures execute faster

②Stored procedures reduce network traffic

③ The stored procedure is regarded as a security mechanism and is fully utilized

 

2. Create a stored procedure

语法 CREATE PROCEDURE sp_name ([ proc_parameter ]) [ characteristics..] routine_body


proc_parameter specifies the parameter list of the stored procedure, the list format is as follows: [IN|OUT|INOUT] param_name type
where in means input parameter, out means output parameter, inout means both input and output;

param_name indicates the parameter name; type indicates the type of the parameter, which can be any type in the MYSQL database


characteristic:
    LANGUAGE SQL
  | [NOT] DETERMINISTIC
  | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
  | SQL SECURITY { DEFINER | INVOKER }
  | COMMENT 'string'

LANGUAGE SQL : indicates that the routine_body part is composed of SQL statements , the language currently supported by the system is SQL, and SQL is the only value of the LANGUAGE feature
[NOT] DETERMINISTIC: Indicates whether the stored procedure execution result is correct. DETERMINISTIC indicates that the result is deterministic. Every time you execute the stored procedure, you get the
same output for the same input.
[NOT] DETERMINISTIC indicates that the result is indeterminate, and the same input may result in different output. If no value is specified, the default is [NOT] DETERMINISTIC
CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA: Specifies the limit for subroutines to use SQL statements.
CONTAINS SQL indicates that the subprogram contains SQL statements, but does not contain statements to read and write data;
NO SQL indicates that the subprogram does not contain SQL statements;
READS SQL DATA: indicates that the subprogram contains statements to read data;
MODIFIES SQL DATA indicates that the subprogram contains write data statement.
By default, the system will specify CONTAINS SQL
SQL SECURITY { DEFINER | INVOKER } : Indicates who has permission to execute. DEFINER means that only the
definer can execute. INVOKER means that the caller with permission can execute. By default, the system is specified as DEFINER
COMMENT 'string': comment information, which can be used to describe stored procedures or functions


routine_body:
    Valid SQL procedure statement or statements
Routine_body is the content of SQL code, you can use BEGIN...END to indicate the beginning and end of SQL code

 

3. Don't be frightened by the above definitions, it's all theory, and practice yields true knowledge. Well, let's take a simple stored procedure, Let's Go

 

delimiter $$
create procedure proc1(out s int)
begin
select count(*) into s from person;
end
$$
delimiter ;

 illustrate:

 

①The delimiter is used here, which means the delimiter. In Mysql, ";" is used as the delimiter by default. If we do not declare it, the compiler will treat the stored procedure as a SQL statement, and the compilation of the stored procedure will report an error. Therefore, use delimiter to declare the delimiter in advance, so that the compiler will treat ";" as the code of the stored procedure, and restore the delimiter after use.

②The stored procedure sometimes needs input, output, input and output parameters. This example uses the output parameter s, which is of type int. If there are multiple parameters separated by commas

③begin and end mark the start and end of the process body

Isn't it very simple, ok go on

 

4. Parameters of the stored procedure

CREATE PROCEDURE([[IN |OUT |INOUT ] parameter name data type...])
IN input parameter: indicates that the value of the parameter must be specified when calling the stored procedure, and the value of the parameter modified in the stored procedure cannot be returned , the default value

 

mysql> set @s=0;
Query OK, 0 rows affected (0.00 sec)

mysql> call proc1(@s);
Query OK, 1 row affected (0.10 sec)

mysql> select @s;
+------+
| @s   |
+------+
|    5 |
+------+
1 row in set (0.00 sec)

 
OUT output parameter: the value can be changed within the stored procedure, and can be returned [refer to IN]
INOUT input and output parameter: specified when calling, and can be changed and returned [refer to IN]

 

 

5. Define variables

DECLARE variable_name [,variable_name...] datatype [DEFAULT value];
其中,datatype为MySQL的数据类型,如:int, float, date, varchar(length)
DECLARE l_int int unsigned default 4000000; 
DECLARE l_numeric number(8,2) DEFAULT 9.95; 
DECLARE l_date date DEFAULT '1999-12-31'; 
DECLARE l_datetime datetime DEFAULT '1999-12-31 23:59:59'; 
DECLARE l_varchar varchar(255) DEFAULT 'This will not be padded';

variable assignment
 SET variable name = expression value [,variable_name = expression ...]

 

6. The stored procedure is called
with call and your procedure name and a bracket. In the brackets, add parameters as needed. The parameters include input parameters, output parameters, and input and output parameters [call proc1 (@S)]

 

7. The query of the stored procedure
can be used select name from mysql.proc where db='database name';[stored procedures and functions]
or select routine_name from information_schema.routines where routine_schema='database name';[stored procedures and functions]
or show procedure status where db='database name';[stored procedure] To
view the details of the current stored procedure, you can use
SHOW CREATE PROCEDURE database.stored procedure name;

 

8 Modifying the stored procedure
ALTER PROCEDURE
Changes the pre-specified stored procedure established with CREATE PROCEDURE, which does not affect the associated stored procedure or stored functionality.
 
9
Deletion of stored procedures Deleting a stored procedure is relatively simple, just like deleting a table:
DROP PROCEDURE
deletes one or more stored procedures from a MySQL table

 

10 The control statement of the stored procedure
(1). The variable
inside the variable scope has a higher priority in its scope, when the execution reaches the end. When the variable is used, the internal variable disappears. At this time, it is outside its scope and the variable is no longer visible. It should be
that the declared variable can no longer be found outside the stored procedure, but you can pass the out parameter or assign its value
to the session variable . to save its value.
 
 
mysql > DELIMITER // 
mysql > CREATE PROCEDURE proc3() 
     -> begin
     -> declare x1 varchar(5) default 'outer'; 
     -> begin
     -> declare x1 varchar(5) default 'inner'; 
     -> select x1; 
     - > end; 
     -> select x1; 
     -> end; 
     -> // 
mysql > DELIMITER ; 
 
 (2). Conditional statement
I. if-then -else statement
 
mysql > DELIMITER // 
mysql > CREATE PROCEDURE proc2(IN parameter int) 
     - >

     -> set var=parameter+1; 
     -> if var=0 then
     -> insert into t values(17); 
     -> end if; 
     -> if parameter=0 then
     -> update t set s1=s1+1; 
     -> else
     -> update t set s1=s1+2; 
     -> end if; 
     -> end; 
     -> // 
mysql > DELIMITER ; 

Ⅱ. case语句:
mysql > DELIMITER // 
mysql > CREATE PROCEDURE proc3 (in parameter int) 
     -> begin
     -> declare var int; 
     -> set var=parameter+1; 
     -> case var 
     -> when 0 then  
     -> insert into t values(17); 
     -> when 1 then  
     ->insert into t values(18); 
     -> else  
     -> insert into t values(19); 
     -> end case; 
     -> end; 
     -> // 
mysql > DELIMITER ;
 
(3). Loop statement
Ⅰ. while · · end while:
mysql > DELIMITER / / 
mysql > CREATE PROCEDURE proc4() 
     -> begin
     -> declare var int; 
     -> set var=0; 
     -> while var<6 do 
     -> insert into t values(var); 
     -> set var=var+1; 
     -> end while; 
     -> end; 
     -> // 
mysql > DELIMITER ;
 
 
Ⅱ. repeat... end repeat:
It checks the result after the operation, while while checks before the execution.
mysql > DELIMITER // 
mysql > 
     -> begin  
     -> declare v int; 
     -> set v=0; 
     -> repeat 
     -> insert into t values(v); 
     -> set v=v+1; 
     -> until v>=5 
     -> end repeat; 
     -> end; 
     -> // 
mysql > DELIMITER ; 
 

Ⅲ. loop ·····end loop:
The loop loop does not need an initial condition, which is similar to the while loop, and does not need an end condition like the repeat loop. The meaning is to leave the loop.
mysql > DELIMITER // 
mysql > CREATE PROCEDURE proc6 () 
     -> begin
     -> declare v int; 
     -> set v=0; 
     -> LOOP_LABLE:loop 
     -> insert into t values(v); 
     -> set v=v+ 1; 
     -> if v >=5 then
     -> 
     -> end if; 
     -> end loop; 
     -> end; 
     -> // 
mysql > DELIMITER ; 
 
 
IV. LABLES labels:
labels can be used before begin repeat while or loop statements, and statement labels can only be used before legal statements. It is possible to break out of the loop and make the run instruction reach the final step of the compound statement.
 
(4). ITERATE iteration
Ⅰ. ITERATE:
start a compound statement from a new one by referring to the label of the compound statement
mysql > DELIMITER // 
mysql > CREATE PROCEDURE proc10 () 
     -> begin
     -> declare v int; 
     -> set v=0; 
     -> LOOP_LABLE:loop 
     -> if v=3 then  
     -> set v=v+1; 
     -> ITERATE LOOP_LABLE; 
     -> end if; 
     -> insert into t values(v); 
     -> set v=v+1; 
     -> if v>

     -> end if; 
     -> end loop; 
     -> end; 
     -> // 
mysql > DELIMITER ;

 

 

SqlServer stored procedure: http://www.cnblogs.com/knowledgesea/archive/2013/01/02/2841588.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327026527&siteId=291194637