1. Stored Procedure1.1. Basic Syntaxcreate procedure name ([params]) UNSIGNED [characteristics] routine_body params: in|out|inout specifies the parameter list representing input and output Routine_body: SQL code content, beginning with "begin" and ending with "end". characteristics: specifies the characteristics of the stored procedure, including 5 types 1 DETERMINISTIC 1.2 Create a stored procedure with specified execution permissionscreate DEFINER=`root`@`%` procedure name ([params]) UNSIGNED [characteristics] routine_body DEFINER: Specifies who has the authority to execute. 1.3 Use of DELIMITER"DELIMITER //" means setting the "//" symbol as the end word, because the default statement end in MySQL is a semicolon ';'. In order to avoid conflicts between stored procedures and MySQL statement symbols, DELIMITER is sometimes used to change the end word symbol, and it should be used in conjunction with end //; Example: Create a stored procedure executed by the root account to output the length of a given string DELIMITER // CREATE definer=`root`@`%` PROCEDURE `avgFruitPrice`( in f_string VARCHAR(200) ) BEGIN select length(f_string); END// 2. Create a functionFunctions are created in the same way as stored procedures Example DELIMITER // CREATE definer=`root`@`%` FUNCTION `my_length`( f_string VARCHAR(200) ) RETURNS INT(11) UNSIGNED NO SQL BEGIN return length(f_string); END// Note: There are three things to note when creating a function. 1. RETURNS: The return type must be specified 2. UNSIGNED NO SQL requires specifying the stored procedure feature 3.return: Return the required data Errors encountered: If the error message above is displayed, it means that the stored procedure characteristics are not specified. In a stored procedure function, you can use the MySQL query result as its parameter: The statement is select .... into begin declare onename char(50) default'0'; declare twoname char(50); select f_name, b_name into onename, twoname from t_user where id =1; ....... end// illustrate: declare: variables defined inside stored procedures and functions default: default value This is the end of this article about creating stored procedures and functions in MySQL. For more relevant MySQL stored procedures and functions, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
>>: Introduction to the pitfalls of Linux high concurrency and performance optimization
This article example shares the specific code for...
Table of contents React upload file display progr...
Table of contents 1. Parameters that determine ca...
This article describes the Linux file management ...
There are always some problems when configuring n...
Table of contents 1. Introduction to Compose 2. C...
Table of contents 01 Common Faults 1 02 Common Fa...
This article mainly introduces how to use the Rea...
js date time format Convert the date and time to ...
This article shares the specific code of Vue to r...
cause I once set up WordPress on Vultr, but for w...
The custom encapsulation code of the vue button c...
The application of containers is becoming more an...
describe: When the Tabs component switches back a...
ab command principle Apache's ab command simu...