How MySQL stored procedure query results are assigned to variables

Source: Internet
Author: User

MySQL stored procedure query results are assigned to variables, the query results are assigned to variables, most of the cases using cursors to complete, but if it is clear that the query results only one row (such as the number of statistical records, a field sum, etc.), you can actually use set or into the way to achieve the assignment. Example code:
Drop Table if existstest_tbl;Create TableTEST_TBL (namevarchar( -), statusint(2));Insert  intoTest_tblValues('ABC',1),('EDF',2),('XYZ',3); Drop procedure IF EXISTSPro_test_3;delimiter//Create procedurepro_test_3 ()begin--Mode 1    DECLARECntINT DEFAULT 0;    Select Count(*) intoCnt fromtest_tbl;    SelectCNT; --Mode 2    Set @cnt =(Select Count(*) fromtest_tbl);    Select @cnt; --Mode 3    Select Count(*) into @cnt1  fromtest_tbl;    Select @cnt1; --multiple columns, it seems that you can only use the into mode    Select Max(status),avg(status) into @max,@avg  fromtest_tbl;    Select @max,@avg;End//delimiter; Call pro_test_3 ();

How MySQL stored procedure query results are assigned to variables

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.