Translation: DECLARE Variable (submitted to the MariaDB official Manual), variablemariadb
This article is a translation of the mariadb official manual: DECLARE Variable.
Original article: https://mariadb.com/kb/en/library/declare-variable/
Https://mariadb.com/kb/zh-cn/declare-variable/ I submitted to the MariaDB official manual
Syntax:
DECLARE var_name [, var_name] ... type [DEFAULT value]
Description
This statement is used to declare local variables in the stored program stored programs. AvailableDEFAULT
Keyword specifies the default value for the variable. The value of a variable can be an expression (or even a subquery) and is not required to be a constant value. If this default value is not specified, it is not necessarily a constant. If you do not specifyDEFAULT
Clause, the initial value of the variable isNULL
.
Local variables are similar to stored procedure parameters and check data types and overflow. For more information, see create procedure.
Local variables must be declared inCONDITION s
, CURSOR s andHANDLER s
Before.
Local variable names are case insensitive.
The effective scope of a local variable is declared.BEGIN ... END
. If a code block contains a nested code block, the nested code can reference a variable of the upper level, unless the variable defined in the inner code block is the same as the upper variable name.
Type of/ROW TYPE
Used in Stored ProceduresTYPE OF
AndROW TYPE OF
The data type anchoring function is introduced from MariaDB 10.3.
You can use another object to define the data type (such as the row in a table), instead of setting it in a declaration statement. If the data type of the anchored object changes, the Data Type changes accordingly. This makes the stored procedure easier to maintain. When the table data type changes, the variable type in the stored procedure also changes automatically.
UseROW TYPE OF
Defined variables imply ROW variables, so they have the same features. Cannot be used in the LIMIT clauseROW TYPE OF
.
TYPE OF
AndROW TYPE OF table_name
The real-time data type can be obtained at the beginning of the called stored procedure. Executing the alter table or drop table statement on the TABLE where the anchored object is stored in the stored procedure does not affect the variable type that has been anchored, even if the variable is defined after the alter table or drop table statement (Note: In the stored procedure, the variable type ends after all the actions are executed ).
ROW TYPE OF cursor_name
The Real-time Data Type of a variable is obtained only when the variable declaration statement is executed. The data type is only anchored once and will not be changed later. IfROW TYPE OF
If a variable is defined in a loop, the data type is obtained at the beginning of the loop, and the subsequent loop does not change.
CheckTYPE OF
AndROW TYPE OF
Whether the specified table exists. However, when creating a stored procedure create procedure or the create function, the system does not check whether the table referenced by the variable exists.
Example
DECLARE tmp type of t1.a; -- obtain the data type declare rec1 row type of t1 Based on the {a} column in table {t1; -- set the ROW data type declare rec2 row type of cur1 In the table {t1}; -- Obtain the ROW data TYPE based on the cursor {cur1}
See Also
Back to Linux series article outline: http://www.cnblogs.com/f-ck-need-u/p/7048359.html
Back to website architecture series article outline: http://www.cnblogs.com/f-ck-need-u/p/7576137.html
Back to database series article outline: http://www.cnblogs.com/f-ck-need-u/p/7586194.html
Reprinted please indicate the source: http://www.cnblogs.com/f-ck-need-u/p/8637747.html
Note: If you think this article is not bad, please click the recommendation in the lower right corner. Your support can stimulate the author's enthusiasm for writing. Thank you very much!