First, stored procedures
The concept : A stored procedure is a group in order to complete a specific feature set of SQL statements, and its essence is a piece of code stored in the database. It can consist of declarative and procedural sql statement sql statement.
Features:
- SQL language can enhance the functionality and flexibility
- Good encapsulation
- high performance
- You can reduce network traffic
- It can be used as a security mechanism to ensure the integrity of the database and data security
User-defined terminator ( ELIMITER )
- ELimit $$:
- Example: The MySQL terminator changed to two exclamation marks "!!" (DELIMITER !!)
Create a stored procedure using the CREATE PROCEDURE statement: CREATE PROCEDURE the SP_NAME ([proc_parameter [, ...]]) routine_body;
proc_paramete: parameter list specifies the stored procedure
routine_body: a body portion stored procedure, also referred to as body of the procedure
[IN | OUT | INOUT] param_name type: parameter name, parameter types
For example: create a stored procedure in mysql_test used to implement a given table customers in a customer id number customers can modify a table in the customer's gender as a specific gender
Use the DECLARE statement to declare local variables: DECLARE var_name [, ...] of the type [the DEFAULT value]
For example: declare a local variable integer cid: DECLARE cid INT (10);
Use DECLARE statement to declare local variables
- 1) Only BEGIN statement in the stored procedure body ... END statement block;
- 2) must be declared at the beginning of the stored procedure;
- 3) its scope is limited to the statement BEGIN ... END statement block;
- 4) different from the user variable
Local variables and user variables difference:
- 1) local variable declarations, not in front of the @ sign, and it can be declared only its BEGIN ... END statement block statement used;
- 2) When you declare a variable user will use the @ symbol in front of its name, user variables declared simultaneously exist in the entire session.
Use of local variable SET statement: SET var_name = expr [, var_name = expr] ... SET CID = 910;
SELECT ... INTO statement using the value of the selected columns directly into a local variable
Flow control statements
- 1, conditional statement IF ... THEN ... ELSE statement CASE statements (IF condition THEN expression the ELSE expression 1 2 END IF;)
- 2, loop LOOP statement WHILE statement REPEAT statement
WHILE END WHILE conditional expression
repeat expression END repeat
loop expression END loop
ITERATE statement that out of the current cycle
**************************** cursor CURSOR ******************** *********************
Create a cursor using the DECLARE CURSOR statement
Use the OPEN statement to open a cursor: OPEN cursor_name
Use FETCH ... INTO statement read data:
Close the cursor, using the CLOSE statement: CLOSE cursor_name
************************ ********************** use stored procedures
Use the CALL statement to call a stored procedure
Sp_update_sex call a stored procedure in the database mysql_test, customer id number 909 modified male sex customers "M"
CALL sp_update_sex(909,’M’);
DROP PROCEDURE statement to remove the use of stored procedures
DROP PROCEDURE[IF EXISTS] sp_name
Second, memory function
Stored function with a stored procedure, an SQL statement is the code fragment and a procedural statements of
Creating stored functions using the CREATE FUNCTION statement
CREATE FUNCTION sp_name([func_parameter[,…]]) RETURNS type routine_body
- sp_name: Specifies the name of the stored function
- func_parameter: parameter specifies the storage function
- RETURNS type: declare function returns the type of data stored value; type specifies the data type of the return value
- Body portion routine_body specified storage function, also called memory function body
Mysql_test created in the database in a storage function, the function can be based on the requirements of a given customer id number returns the customer's gender, if the database is not given customer id number, then return "is not the client."
Use mysql_test; DELIMITER $$ CREATE FUNCTION fn_search(cid INT) RETURNS CHAR(20) DETERMINISTIC BEGIN DECLARE SEX CHAR(20); SELECT cust_sex INTO SEX FROM customers WHERE cust_id=cid; IF SEX IS NULL THEN RETURN(SELECT’没有该客户’); ELSE IF SEX=‘F’ THEN RETURN(SELECT’女’); ELSE RETURN(SELECT ‘男’); END IF; END IF; END $$
Use the keyword SELECT call a stored function: SELECT the SP_NAME ([func_parameter [, ...]])
Use DROP FUNCTION statement removes the stored function: DROP FUNCTION [IF EXISTS] the SP_NAME