What are the declare statements in MySQL
In this issue, the editor will bring you about the declare sentences in MySQL. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
What are the uses of MySQLdeclare statements?
(1) ExamplewithtwoDECLAREstatements
Two instances of DECLARE statements
CREATEPROCEDUREp8 () BEGINDECLAREaINT;DECLAREbINT;SETa=5;SETb=5;INSERTINTOtVALUES (a); SELECTs1*aFROMtWHEREs1 > = bposition End.
The variables defined in the process are not really defined, you just define them in the BEGIN/END block. Note that these variables are different from session variables. You cannot use the modifier @ you must clearly declare variables and their types in the BEGIN/END block. Once a variable is declared, you can use it anywhere you can use session variables, text, and column names.
(2) ExamplewithnoDEFAULTclauseandSETstatement
No examples of default clauses and setting statements
CREATEPROCEDUREp9 () BEGINDECLAREaINT/*thereisnoDEFAULTclause*/;DECLAREbINT/*thereisnoDEFAULTclause*/;SETa=5;/*thereisaSETstatement*/SETb=5;/*thereisaSETstatement*/INSERTINTOtVALUES (a); SELECTs1*aFROMtWHEREs1 > = bposition End.
There are many ways to initialize variables. If there is no default clause, the initial value of the variable is NULL. You can use the set statement to assign values to variables at any time.
(3) ExamplewithDEFAULTclause
An example with a DEFAULT clause
CREATEPROCEDUREp10 () BEGINDECLAREa,bINTDEFAULT5;INSERTINTOtVALUES (a); SELECTs1*aFROMtWHEREs1 > = bposition / end /
We have made some changes here, but the result is still the same. Here the DEFAULT clause is used to set the initial value, so there is no need to separate the implementation of the DECLARE and SET statements.
What are the uses of MySQLdeclare statements?
(4) ExampleofCALL
An example of a call
Mysql > CALLp10 () / / +-+ | s1yoga | +-+ | 25 | 25 | +-+ 2rowsinset (0.00sec) QueryOK,0rowsaffected (0.00sec)
The results show that the process can work properly.
(5) Scope
Scope
CREATEPROCEDUREp11 () BEGINDECLAREx1CHAR (5) DEFAULT'outer';BEGINDECLAREx1CHAR (5) DEFAULT'inner';SELECTx1;END;SELECTx1;END;//
These are the declare statements in the MySQL shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.