Mysql creates a stored procedure and function. A stored procedure is a set of SQL statements that can be stored on the server. You can use create procedure and create function on the client to CREATE a stored PROCEDURE or FUNCTION. CALL the created stored procedure or function through the CALL Statement on the client. Syntax: CREATE a storage Subroutine: 01 create procedure sp_name ([proc_parameter [,...]) 02 [characteristic...] routine_body03CREATE FUNCTION sp_name ([func_parameter [,...]) 04 RETURNS type05 [characteristic...] routine_body06 proc_parameter: 07 [IN | OUT | INOUT] param_name type08 func_parameter: 09 param_name type10 www.2cto.com type: 11 Any valid MySQL data type12characteristic: 13 LANGUAGE SQL14 | [NOT] DETERMINISTIC15 | {contains SQL | no SQL | reads SQL data | modifies SQL data} 16 | SQL SECURITY {DEFINER | INVOKER} 17 | COMMENT 'string' 18routine _ body: 19 Valid SQL procedure statement or statements
By default, subprograms are associated with the current database. To explicitly associate a subroutine with a given database, you can specify the name db_name.sp_name when creating a subroutine. When the program is defined, use the mysql client delimiter command to change the statement delimiter from; //. This is allowed to be used in the program body; the delimiters are passed to the server rather than explained by mysql itself. For example: 01 mysql> delimiter // 02 mysql> create procedure simpleproc (OUT param1 INT) 03-> BEGIN04-> select count (*) INTO param1 FROM t; 05-> END www.2cto.com 06-> // 07 Query OK, 0 rows affected (0.00 sec) 08 mysql> delimiter; 09 mysql> CALL simpleproc (@ a); 10 Query OK, 0 rows affected (0.00 sec) 11 mysql> SELECT @; 12 + ------ + 13 | @ a | 14 + ------ + 15 | 3 | 16 + ------ + 171 row in set (0.00 sec) 01 mysql> delimiter // 02mys Ql> create function hello (s CHAR (20) returns char (50) 03-> return concat ('hello, ', s ,'! '); 04-> // 05 Query OK, 0 rows affected (0.00 sec) 06 mysql> delimiter; 07 www.2cto.com mysql> SELECT hello ('World '); 08 + ---------------- + 09 | hello ('World') | 10 + ---------------- + 11 | Hello, world! | 12 + ---------------- + 131 row in set (0.00 sec) Author honey