Use select... The into statement assigns a value to the variable.

Source: Internet
Author: User
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:

    1. 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:

    1. CreateProcedure Getmsg
    2. ()
    3. Begin
    4. DeclareV_titleVarchar(30 );
    5. DeclareV_contentVarchar(100 );
    6. SelectTitle, contentInto V_title, v_contentFromNewsWhere Artid = 333;
    7. 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:

    1. Create procedure getmsg
    2. ()
    3. begin
    4. declare v_title varchar (30);
    5. declare v_content varchar (100);
    6. select title, content into v_title, v_content from News where artid = 333;
    7. select v_title , v_content ;
    8. end

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.