DROPPROCEDUREIFEXISTS insert_while;#创建存储过程delimiter//CREATEPROCEDURE insert_while()BEGINDECLARE i INT;SET i =1;WHILE i <=10DOINSERTINTO`student`(`id`,`user_name`,`age`,`dept`,`class`,`remark`)VALUES(CONCAT(10000+ i), CONCAT('张先生', i), i,"第一年级",'一班','北京市海淀区');SET i = i +1;ENDWHILE;END//delimiter;#执行存储过程CALL insert_while();#删除存储过程DROPPROCEDUREIFEXISTS insert_while;
#删除存储过程DROPPROCEDUREIFEXISTS insert_while;#创建存储过程delimiter//CREATEPROCEDURE insert_while()BEGINDECLARE i INT;DECLARE tempUUID text;SET i =1;WHILE i <=10DOSET tempUUID = UUID();INSERTINTO`user`(`id`,`name`)VALUES(tempUUID,CONCAT('张先生', i));INSERTINTO`student`(`id`,`user_name`,`age`,`dept`,`class`,`remark`)VALUES(tempUUID, CONCAT('张先生', i), i,"第一年级",'一班','北京市海淀区');SET i = i +1;ENDWHILE;END//delimiter;#执行存储过程CALL insert_while();#删除存储过程DROPPROCEDUREIFEXISTS insert_while;
DROPFUNCTIONIFEXISTS randGetID;CREATEFUNCTION randGetID()RETURNStextBEGINDECLARE i INT;DECLARE j INT;DECLARE tempID text;SELECTCOUNT(*)INTO i FROM student;IF(i>0)THENSET j=FLOOR(RAND()*i);SELECT id INTO tempID FROM student LIMIT j,1;ELSESET tempID=UUID();ENDIF;RETURN tempID;END
优化
DROPFUNCTIONIFEXISTS randGetID;CREATEFUNCTION randGetID()RETURNStextBEGINDECLARE tempID text;SELECT id INTO tempID FROM student ORDERBY RAND()LIMIT1;IF( tempID ISNULL)THENRETURN UUID();ENDIF;RETURN tempID;END
1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)