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