This is the official manual of MARIADB: DECLARE variable.
Original:https://mariadb.com/kb/en/library/declare-variable/
I submit it to MARIADB official manual:https://mariadb.com/kb/zh-cn/declare-variable/
Grammar:
DECLARE var_name [, var_name] ... type [DEFAULT value]
Describe
The statement is used in the statement to declare a local variable in the stored program stored programs . You can DEFAULT
specify a default value for a variable with a keyword. The value of a variable is allowed to be an expression (even a subquery) and does not require that it must be a constant value. If you do not specify this default value, it is not necessarily a constant. If you do not specify a DEFAULT
clause, the initial value of the variable is NULL
.
Local variables are similar to stored procedure parameters, and they are checked for data types and overflows. For more information, refer to CREATE PROCEDURE.
Local variables must be declared on CONDITION s
, CURSOR s , and HANDLER s
before.
Local variable names are case insensitive.
The scope of the local variable is declared within it BEGIN ... END
. If a block of code is nested within a code block, the code in the nesting can refer to the previous level of the variable, unless the inner code block defines the same variable name and the upper-layer variable name.
Type Of/row type of
The TYPE OF
ability to use and ROW TYPE OF
anchor data types in stored procedures is introduced from MariaDB 10.3.
Anchoring a data type allows you to define data types (such as rows in a table) based on another object without having to specifically set them in a declaration statement. If the data type of the anchored object changes, it also changes itself. This makes stored procedures easier to maintain, and variable types in stored procedures automatically change when the data types in the table change.
A ROW TYPE OF
ROW variable is implied by using a defined variable, so they have the same characteristics. cannot be used in the LIMIT clause ROW TYPE OF
.
TYPE OF
And ROW TYPE OF table_name
the real-time data type can be obtained at the beginning of the call to the stored procedure. An alter table or drop table statement in a stored procedure on the same table as the anchored object does not affect the type of the variable that is anchored, even if the variable is defined in ALTER TABLE or drop after the TABLE statement (Translator Note: Because the type of the variable is already anchored to the end before all actions are executed in the stored procedure).
ROW TYPE OF cursor_name
The real-time data type of a variable is obtained when a variable declaration statement is executed. The data type is only anchored once, and then no longer changes. If a variable in ROW TYPE OF
a cursor is defined in a loop, the data type is acquired at the beginning of the loop, and the subsequent loop no longer changes.
At the beginning of the call to the stored procedure, the table is checked TYPE OF
and the ROW TYPE OF
anchor is present. However, when creating a stored procedure create PROCEDURE or function create, the table referenced by the variable is not checked for existence.
Example
DECLARE tmp TYPE OF t1.a;-- 基于表{{t1}}中的{{a}}列获取数据类型DECLARE rec1 ROW TYPE OF t1;-- 锚定表{{t1}}中行数据类型DECLARE rec2 ROW TYPE OF cur1;-- 基于游标{{cur1}}获取行数据类型
See Also
Back to Linux series article outline: http://www.cnblogs.com/f-ck-need-u/p/7048359.html
Back to Site 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
Reprint Please specify source: http://www.cnblogs.com/f-ck-need-u/p/8637747.html
Note: If you think this article is not bad please click on the lower right corner of the recommendation, your support can inspire the author more enthusiasm for writing, thank you very much!
Translation: DECLARE Variable (submitted to MARIADB official manual)