Use select... The into statement assigns a value to the variable.
In the MySQL stored procedure, you can use select... The into statement assigns a value to the variable, queries the variable in the database, and assigns the obtained result to the variable. Select... The syntax format of the into statement is as follows:
- SelectCol_name [,...]IntoVar_name [,...]Table_expr
Col_name: Name of the column field to be queried from the database;
Var_name: Variable name. The column field name corresponds to the position in the column list and the variable list, and the queried value is assigned to the variable at the corresponding position;
Table_expr: The Rest Of The SELECT statement, including the optional from clause and where clause.
Note that when using select... In the into statement, the variable name cannot be the same as the field name in the data table. Otherwise, an error occurs. Example Statement:
- CreateProcedure Getmsg
- ()
- Begin
- DeclareV_titleVarchar(30 );
- DeclareV_contentVarchar(100 );
- SelectTitle, contentInto V_title, v_contentFromNewsWhere Artid = 333;
- End
Return the variable value to the caller.
After processing the variables defined in the stored procedure, the result value may be returned to the stored procedure caller. So how to return? It is convenient to use the SELECT statement to return variables as the result set. ThereforeCodeAdd the following sentence:
- Create procedure getmsg
- ()
- begin
- declare v_title varchar (30);
- declare v_content varchar (100);
- select title, content into v_title, v_content from News where artid = 333;
- select v_title , v_content ;
- end