Translation: DECLARE Variable (submitted to the MariaDB official Manual), variablemariadb

Source: Internet
Author: User

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. AvailableDEFAULTKeyword 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 specifyDEFAULTClause, 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 sBefore.

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 OFAndROW TYPE OFThe 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 OFDefined variables imply ROW variables, so they have the same features. Cannot be used in the LIMIT clauseROW TYPE OF.

TYPE OFAndROW TYPE OF table_nameThe 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_nameThe 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 OFIf 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 OFAndROW TYPE OFWhether 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
  • User-Defined variables

 

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.