mysql stored procedures, functions,

This post last edited by snowster (0) in 1.1 2019-7-18 18:46 stored procedures, functions described - the difference between stored procedures and functions: no return value stored procedure, and must have the function; parameter storage process may be used IN, OUT, INOUT type, the parameter type iN function only. - Characteristics of stored procedures - to complete more complex computation and determination, and the processing logic is encapsulated in the database side, the caller does not need to deal with their own business logic, once changes in logic, only need to modify the stored procedure can, while the callee no impact. - Programmable strong, flexible - SQL programming code reusable - relatively faster execution speed - reducing the data transmission between the network, to save money to create a stored procedure 1.2 - - Create a stored procedure DELIMITER $$ CREATE PROCEDURE testa () BEGIN SELECT * FROM student WHERE id = 2; END $$ - call a stored procedure call testa (); 1.3 check stored procedure / function, deleted - Charles - View status stored procedure or function SHOW pROCEDURE sTATUS LIKE 'testa'; - View stored procedure or function defined SHOW CREATE pROCEDURE testa- delete - delete a stored procedure DROP pROCEDURE testa1; - delete function DROP fUNCTION testa1; variable 1.4 stored procedures - needs: write stored procedures, use variables to take id = 2 username; variable my_uname DELIMITER $$ CREATE PROCEDURE testa3 () BEGIN - statement 1. declare variables used, a statement only declare a variable, the variable must be declared after use - 2. Having a variable data type and length, consistent with the mysql SQL data types, and even so a default value is specified, character set and sort rules DECLARE my_uname VARCHAR (32) DEFAULT ''; - 3. variables can be assigned by a set , may be assigned by way of select into SET my_uname = 'itheima'; SELECT nAME INTO my_uname FROM student WHERE id = 2; - 4. variables need to return, select statement can be used, such as: select a variable name SELECT my_uname; END $ $ CALL testa3 (); - variable scope - scope of the variable is, between the begin and scope end block, the end of the end of the variable scope is finished. - need to pass values ​​between a plurality of blocks, can use global variables, i.e. on all code blocks before. - parameter passing are global variables, can act between a plurality of blocks stored procedure parameters passed 1.5 IN type - for example - Demand: write stored procedures, incoming id, returns the user's name DELIMITER $$ CREATE PROCEDURE getName ( my_uid INT) BEGIN DECLARE my_uname VARCHAR (32) DEFAULT ''; SELECT NAME INTO my_uname FROM student WHERE id = my_uid; SELECT my_uname; END; $$ CALL getName (2); - passing parameters: type IN, It represents the value of this parameter must be specified when calling a stored procedure, if not explicitly specified as IN, then the default is IN type. - the IN type parameter is generally used for incoming, call a stored procedure is generally not be modified and returned - if desired to modify the stored procedure call and return values ​​may be used 1.6 OUT type parameters stored procedure parameter OUT outgoing type - for example - demand: calling a stored procedure to return the user's incoming uid uname DELIMITER $$ CREATE pROCEDURE getName22 (iN my_uid INT, OUT my_uname VARCHAR (32)) BEGIN SELECT NAME INTO my_uname FROM student WHERE id = my_uid; SELECT my_uname; END; $$ - Specifies whether incoming parameter argument SET @uname: = ''; cALL getName22 (2, @ uname); - aliases SELECT @uname AS myName; - outgoing parameters: call a stored procedure, which can be changed value, and return - OUT is spread parameter, can not be used to pass a parameter value - when you call a stored procedure, OUT parameters also need to specify, but it must be a variable, not a constant - if both need to pass at the same time need to pass out, you can use the 1.7 INOUT type parameters stored procedure INOUT variable parameter type - for example: - demand: calling a stored procedure parameter my_u id and my_uname, both incoming and also outgoing parameter DELIMITER $$ CREATE PROCEDURE getName3 (INOUT my_uid INT, INOUT my_uname VARCHAR (32)) BEGIN SET my_uid = 2; SET my_uname = 'hxf3'; SELECT id, NAME INTO my_uid, my_uname FROM student WHERE id = my_uid; SELECT my_uid, my_uname; END; $$ SET @uname: = ''; SET @uid: = 0; CALL getName3 (@ uid, @ uname); SELECT @uname AS myName; - variable variable INOUT: the value of the call can be passed, during the call, its value can be modified, but also to the return value. - when an incoming call is INOUT variable, not a constant 1.8 stored procedure conditional statements - for example: - Demand: write stored procedures, user uid is even if it is given uname, otherwise only return uid DELIMITER $$ CREATE PROCEDURE getName44 (IN my_uid INT) BEGIN DECLARE my_uname VARCHAR (32) DEFAULT ''; IF (my_uid% 2 = 0) THEN SELECT NAME INTO my_uname FROM student WHERE id = my_uid; SELECT my_uname; ELSE SELECT my_uid; END IF; END; $ $ CALL getName44 (1); CALL getName44 (2); - the basic structure of conditional statements: if () then ... else ... end if; 1. Loops 9 stored procedure - the while loop - Example: - Demand: Use loop, 10 is inserted into the consecutive recording table Users uid (uid) in. DELIMITER $$ CREATE PROCEDURE insertdata () BEGIN DECLARE i INT DEFAULT 0; WHILE (i <10) DO BEGIN SELECT i; SET i = i + 1; INSERT INTO users (NAME, address) VALUES ( "Monkey", "Guangzhou" ); END; END WHILE; END; $$ CALL insertdata (); - while the basic sentence structure: while () do ... end while; - while Analyzing logic returns true or false, the expression may be any return true or false expression - repeat loop - Example: - demand: using a repeat loop is inserted into the table Article uid users 10 consecutive recording DELIMITER $$ CREATE PROCEDURE insertdata2 () BEGIN DECLARE i INT DEFAULT 100; REPEAT BEGIN SELECT i; SET i = i + 1; INSERT INTO users (NAME) VALUES ( 'dark horse'); END; UNTIL i> = 110 END REPEAT; END; $$ CALL insertdata3 (); - repeat the basic sentence structure: repeat ... until ... end REPEAT; - until Analyzing logic returns true or false, the expression may be any expression that returns true or false until statement is only when true, the loop ends. 1.10 cursor (cursor) using substantially - the stored procedures and functions, can use the cursor (also sometimes referred to as a cursor) on the result set treatment cycle - the basic use - the cursor stated: cursor for - opening Cursor: Open - Move cursor: FETCH - Close cursor: close- Example: - memory write procedure, using the cursor, the record id is even individually update user name. DELIMITER $$ CREATE PROCEDURE testcursor () BEGIN - controlling cursor cycle end flag DECLARE stopflag INT DEFAULT 0; DECLARE my_uname VARCHAR (20); - cursor for cursor stated DECLARE uname_cur CURSOR FOR SELECT NAME FROM student WHERE id% 2 = 0; DECLARE CONTINUE HANDLER FOR NOT FOUND SET stopflag = 1; oPEN uname_cur; - open a cursor FETCH uname_cur iNTO my_uname; - cursor step forward, taken in a record into variable my_uname. WHILE (stopflag = 0) DO - if the cursor to the end of yet, continue BEGIN UPDATE student SET NAME = CONCAT (my_uname, ' _cur ') WHERE NAME = my_uname; - cursor step forward, taken in a record into variable my_uname. FETCH uname_cur INTO my_uname; END; END WHILE; CLOSE uname_cur; END; $$ DELIMITER; - call CALL testcursor () - Note: variable conditions, the processing program, the cursor is defined by a DECLARE, between which there is order requirements, variables and conditions must be in the forefront statement before the cursor is declared, and finally can be stated handler. 




image.png

Guess you like

Origin blog.51cto.com/14500648/2430031