--************** Three, stored procedure *******************- -- declaration Terminator -- Create a stored procedure DELIMITER $ CREATE PROCEDURE pro_test () BEGIN -- can write multiple SQL statements ; SELECT * from employee; END $ -- Execute stored procedure Call Pro_test (); --3.1 stored procedure with input parameters -- requirements: Pass in an employee ID, query employee information DELIMITER $ CREATE PROCEDURE Pro_findbyid (in Eid Int.)--In: input parameters BEGIN SELECT * FROM employee WHERE Id=eid; END $ -- Call Call Pro_findbyid (4); --3.2 stored procedure with output parameters DELIMITER $ CREATE PROCEDURE pro_testout (out str VARCHAR) –out: Output parameters BEGIN -- assigning values to parameters SET str= ' Helljava '; END $ -- Delete stored procedures DROP PROCEDURE pro_testout; -- Call -- How to accept the value of the return parameter?? --***mysql variable ****** -- global variables (built-in variables):mysql database built-in variables (all connections work) -- View all global variables: Show Variables -- View a global variable: select @@ 变量 name -- Modify global variables: set variable name = New value -- encoding of the received data of the CHARACTER_SET_CLIENT:MYSQL server --Character_set_results: encoding of MySQL server output data -- Session variable: exists only in one connection between the current client and the database server side. If the connection is broken, then all session variables are lost! -- Define session Variables : SET @ variable = value -- View Session variables: SELECT @ variable -- Local variables: variables used in stored procedures are called local variables. Local variables are lost as long as the stored procedure is executed!! --1) define a Session variable name, 2) use the name Session variable to receive the return value of the stored procedure Call Pro_testout (@NAME); -- View variable values SELECT @NAME; --3.3 stored procedure with input and output parameters DELIMITER $ CREATE PROCEDURE pro_testinout (INOUT n INT)--INOUT: input and Output parameters BEGIN -- View variables SELECT N; SET n = 500; END $ -- Call SET @n=10; Call Pro_testinout (@n); SELECT @n; --3.4 a stored procedure with conditional judgment - -demand: Enter an integer, if 1, return "Monday" , if 2, return "Tuesday", if 3 , return to "Wednesday". Other numbers, return "error input"; DELIMITER $ CREATE PROCEDURE pro_testif (in num int,out str VARCHAR (20)) BEGIN IF Num=1 Then SET str= ' Monday '; ELSEIF num=2 Then SET str= ' Tuesday '; ELSEIF Num=3 Then SET str= ' Wednesday '; ELSE SET str= ' input error '; END IF; END $ Call Pro_testif (4, @str); SELECT @str; --3.5 stored procedure with cyclic function -- requirements: Enter an integer and sum. For example, the input of the 1-100 and the statistical DELIMITER $ CREATE PROCEDURE pro_testwhile (in num int,out result INT) BEGIN -- Define a local variable DECLARE i INT DEFAULT 1; DECLARE vsum INT DEFAULT 0; While I<=num do SET vsum = vsum+i; SET i=i+1; END while; SET result=vsum; END $ DROP PROCEDURE Pro_testwhile; Call Pro_testwhile (@result); SELECT @result; Use DAY16; --3.6 Using the result of a query to assign a value to a variable (into) DELIMITER $ CREATE PROCEDURE Pro_findbyid2 (in Eid int,out vname VARCHAR (20)) BEGIN SELECT EmpName to VName from employee WHERE Id=eid; END $ Call Pro_findbyid2 (1, @NAME); SELECT @NAME; |