--------------Create a stored procedure with no parameters-----------------DELIMITER;;Drop PROCEDURE if EXISTSselectstudent;Create PROCEDURE 'selectstudent'()BEGINSelect* fromstudent;End;;D Elimiter;--------------A stored procedure with parameters--------------------Drop PROCEDURE if EXISTSselectcity;Create PROCEDURE 'selectcity'(inch_cityidvarchar(Ten)--input Parameters--)BEGINSelect * fromStudentwhereCityid=_cityid;End;--------------A stored procedure with an output parameter--------------------Drop PROCEDURE if EXISTSSelectcity_name;CREATE PROCEDURE 'Selectcity_name'( inch_cityidvarchar(Ten)--input parameters,Out _cityvarchar(Ten)--output parameters,InOut _cityidnamevarchar(Ten)--input and OUTPUT parameters)BEGINSelect* fromStudentwhereCityid=_cityid andCityName=_cityname into_city;End;Set @_cityid='1';Set @_cityidname='Zhengzhou'; call Selectcity_name (@_cityid,@_city,@_cityidname);Select @_cityidname asId@_city;-----------A stored procedure with wildcard characters------------Drop PROCEDURE if EXISTSSelectcitylike;Create PROCEDURE 'Selectcitylike'(inch_citynamevarchar(Ten))BEGIN Set @exec_sql =CONCAT ("Select * fromStudentwhereName like '% ", _cityname,"%'");PREPAREstmt from @exec_sql;--definitionEXECUTEstmt--executing a preprocessing statementdeallocate PREPAREstmt--Delete DefinitionEnd-------Loop statement: Check results before Operation----------Create PROCEDUREproc4 ()BEGINDeclare var int;Set var=0; while var<6 DoInsert intoTVALUES(var);Set var=var+1;End while;End----------Loop statement: Check results after Operation------------Create PROCEDUREproc5 ()BEGINDECLAREVint;SetV=0; REPEATInsert intoTVALUES(v);SetV=V+1; UNTIL v>=5Endrepeat;End----------Loop statement: Loop. Endloop------------Create PROCEDUREproc6 ()BEGINDeclareVint;SetV=0; Loop_lable:loopInsert into Values(v);SetV=V+1;ifV>=5 ThenLEAVE loop_lable;End if;EndLoop;End----------Loop statement: Loop. Endloop-------------Create PROCEDUREProc7 ()BEGINDECLAREVint;SetV=0; Loop_lable:loopifV=3 ThenSetV=V+1; Iterate loop_lable;--Continue loopingEnd if;Insert intoTValues(v);SetV=V+1; ifV>=5 Thenleave loop_lable;--Jump out of the loop End if;EndLoop;End;
My SQL stored procedures are basically used