This article mainly introduces the MySql stored procedure and function details, which are very detailed. if you need it, you can refer to the stored procedure and function as a set of SQL statements defined in the database, then, you can directly call these stored procedures and functions to execute the defined SQL statements. Stored procedures and functions can prevent developers from writing the same SQL statement repeatedly. In addition, stored procedures and functions are stored and executed on the MySQL server, which can reduce data transmission between the client and the server.
The code is as follows:
Alter procedure proc_name [characteristic…]
Only the features of a stored procedure can be modified. to modify the content of a stored procedure, you can delete the stored procedure and recreate it.
1.7 delete a stored procedure
The code is as follows:
Drop procedure [if exists] sp_name;
II. Functions
2.1 Definition
In MySQL, the basic form of creating a storage FUNCTION is as follows: create function sp_name ([func_parameter [,...]) RETURNS type [characteristic...] routine_body Return
Clause is used to declare the data type that stores the function return value. A stored procedure is a collection of user-defined SQL statements involving tasks of a specific table or other objects. you can call the stored procedure. a function is usually a database-defined method, it receives parameters and returns some type of values, and does not involve specific user tables.
Call storage functions
Select sp_name([func_parameter…]) Select fn_search(2);
Delete storage function drop
Modify the storage function alter to modify some related features of the storage function.
2.2 function examples
(Compare the size and return a large number)
/** Use the FUNCTION **/create function sp_cal_max (p_num1 INT, p_num2 INT) returns int begin if p_num1> = p_num2 then return p_num1; else return p_num2; end if; END
Call:
SET @p_num1=2; SET @p_num2=34; SELECT sp_cal_max(@p_num1,@p_num2);
2.3 Differences between stored procedures and functions
1) In general, the functions implemented by stored procedures must be more complex, while the functions implemented by functions are more targeted. Stored procedures are powerful and can be used to perform a series of database operations, including modifying tables. user-defined functions cannot be used to perform a group of operations to modify the global database status.
2) for stored procedures, parameters such as record sets can be returned, while functions can only return values or table objects. A function can return only one variable, while a stored procedure can return multiple variables. Stored Procedure parameters can be IN, OUT, and INOUT, while functions can only have IN classes ~~ The stored procedure declaration does not require a RETURN type, but the RETURN type must be described during the function declaration. the Function body must contain a valid RETURN statement.
3) stored procedures, you can use undefined functions, and do not allow built-in undefined functions in the user-defined function body.
4) the stored procedure is generally executed as an independent part (EXECUTE statement execution), and the function can be called as a part of the query statement (SELECT call ), because a function can return a table object, it can be located behind the FROM keyword in the query statement. Stored procedures are unavailable in SQL statements, but functions can be used.
3. cursor)
3.1 Definition
The query statement may query multiple records. you can use the cursor label in the stored procedure and function to read records in the query result set one by one. The use of the cursor includes declaring the cursor, opening the cursor, using the cursor, and closing the cursor. The cursor must declare, open, use, and close the cursor. The cursor must be declared before the handler and after the variables and conditions.
1. declare the cursor
Declare cursor_name cursor forselect_statement; Cursor_name: name of the cursor Select_statement: content of the select statement Declare cur_employee cursor forselect name, age from employee;
2 Open the cursor
Open cursor_name Open cur_employee;
3. use the cursor
Mysql uses the fetch keyword to use the cursor, in the syntax format
Fetch cur_name invalid var_name [, var_name…]; Cur_name indicates the name of the cursor Var_name, which stores the information queried by the select statement in the cursor into this parameter. Var_name must be defined before the cursor is declared. Fetch cur_employee emp_name, emp_age;
4 Close the cursor
Close cursor_name; Close cur_employee;
When the cursor is no longer needed, it should be closed. Using the close statement will release all resources used by the cursor. After a cursor is closed, it cannot be used if it is not re-opened. For declared cursors, you do not need to declare them again. you can open them directly using the open statement.
3.2 Examples
(Copy the table test_cur1 data to test_cur2)
CREATE TABLE `test_cur1` ( `id` int(11) NOT NULL auto_increment, `type` char(11) default NULL, `order1` char(11) default NULL, PRIMARY KEY (`id`) ) INSERT INTO `test_cur1` VALUES (1, '145', 'd1'); INSERT INTO `test_cur1` VALUES (2, '134', '1d'); INSERT INTO `test_cur1` VALUES (3, '123', '1ad'); INSERT INTO `test_cur1` VALUES (4, '121', '1as'); CREATE TABLE `test_cur2` ( `id` int(11) NOT NULL auto_increment, `type` char(11) default NULL, `order1` char(11) default NULL, PRIMARY KEY (`id`) )
Then write the cursor:
Create procedure get_cur () begin declare done int default 0; declare id int (11); DECLARE type char (11); DECLARE order1 char (11 ); DECLARE mycur cursor for select * FROM test_cur1; // define the cursor declare continue handler for sqlstate '2013' SET done = 1; // OPEN the cursor open mycur; // start loop repeat fetch mycur into id, type, order1; // retrieve the content of the cursor to the temporary variable if not done then insert into test_cur2 VALUES (ID, type, order1 ); // Insert to another table end if; UNTIL done end repeat; // when done = 1, END the loop // CLOSE the cursor CLOSE mycur; END
Run:
call get_cur()
Let's take a look at the data in two tables: this is Table 2.
This is table 1
The data has been copied successfully.
The above is all the content of this article. I hope it will help you learn MySql stored procedures and functions.