Stored procedures and functions
Stored procedures and functions: A similar procedure in java
- benefit
- Improve code reusability
- Streamline operations
During storage
-
Meaning: a set of pre-compiled collection of sql statement, understand the process batch statement
-
benefit:
- Improve code reusability
- Streamline operations
- Reduce compile times and reduces the number and connect to the database server, improving efficiency
-
Creating grammar
-
CREATE PROCEDURE 储存过程名(参数列表) BEGIN 存储过程体; END
-
-
Parameter structure
- Parameter list consists of three parts
- Parameter name Parameter type Parameter mode
- IN atuname VARCHAR(20)
- Parameter list consists of three parts
-
Parameter Mode
- IN: This parameter can be used as an input
- OUT: This parameter can be used as an output, the return value is
- INOUT: This parameter either as an input and as an output, which is the urgent need to pass the parameter value, and can return values
-
note:
- If the bank only one sentence, BEGIN END may be omitted
- End of the body of the procedure required for each sql statement must semicolon
- End tag
- DELIMITER end tag
-
transfer
-
CALL 存储过程名(实参列表);
-
-
Examples
-
delimiter $ create procedure adm1() begin insert into admin value (null,"lxc","123456"), (null,"aaa","123456"), (null,"bbb","123456"), (null,"ccc","123456"); end $
-
call adm1;
-
-
Delete stored procedure
-
grammar
-
DROP PROCEDURE 存储过程名;
-
-
-
View information stored procedure
-
SHOW CREATE PROCEDURE 存储过程名;
-
function
-
Meaning: a collection of precompiled sql statement, understood as a batch statement
-
Feature
- There is one and only one return value
- parameter list
- Parameter name Parameter type
- Function body: there must be a return statement, if there is no will complain
- If retuen statement is not placed at the end of the function body not being given, but is not recommended
- Only one sentence function body, can be omitted degin end
- Use delimiter set the mark
-
grammar
-
CREATE FUNCTION 函数名(参数列表) RETURNS 返回类型 BEGIN 函数体 END
-
-
transfer
-
SELECT 函数名(参数列表);
-
-
View function
-
SHOW CREATE FUNCTION 函数名;
-
-
Delete function
-
DROP FUNCTION 函数名;
-