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:
01drop tableifexists test_tbl;02CREATE TABLE TEST_TBL (name varchar (), statusint(2)); 03INSERT into TEST_TBL values (' abc ', 1), (' EDF ', 2), (' XYZ ', 3); 04 05drop procedure IF EXISTS pro_test_3;06delimiter//
07CREATE PROCEDURE Pro_test_3 ()08begin09--Way 1 10DECLARE cnt INT DEFAULT0;
11Select COUNT (*) into CNT from TEST_TBL; 12Select CNT;13 14--Way 2 15Set @cnt= (SELECT COUNT (*) from TEST_TBL); 16Select @cnt;17 18--Way 3 19Select COUNT (*) into @cnt1 from TEST_TBL; 20Select @cnt1;21 22--multiple columns, it seems that you can only use the into mode23select Max (status), AVG (status) into the @max, @avg from Test_tbl;24Select @max, @avg;25End26//
27delimiter;28 29Call 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.