MySQL帶參數的存儲過程小例子 存儲過程P_GET_CLASS_NAME是根據輸入的班級號判斷班級名稱 存儲過程P_INSERT_STUDENT是接收輸入的學生信息,最終將信息插入學生表。 www.2cto.com [sql] DROP PROCEDURE IF EXISTS `P_GET_CLASS_NAME`; CREATE PROCEDURE P_GET_CLASS_NAME(IN ID int,OUT NAME VARCHAR(50)) BEGIN IF(ID = 1) THEN SET NAME = '一班'; END IF; IF(ID = 2) THEN SET NAME = '二班'; END IF; END; DROP PROCEDURE IF EXISTS `P_INSERT_STUDENT`; CREATE PROCEDURE P_INSERT_STUDENT(IN ID INT,IN NAME VARCHAR(10),IN CLASSNO INT,IN BIRTH DATETIME) BEGIN SET @ID = ID; SET @NAME = NAME; SET @CLASSNO = CLASSNO; SET @BIRTH = BIRTH; SET @CLASSNAME = NULL; CALL P_GET_CLASS_NAME(@CLASSNO,@CLASSNAME); SET @insertSql = CONCAT('INSERT INTO TBL_STUDENT VALUES(?,?,?,?)'); PREPARE stmtinsert FROM @insertSql; EXECUTE stmtinsert USING @ID,@NAME,@CLASSNAME,@BIRTH; DEALLOCATE PREPARE stmtinsert; END; CALL P_INSERT_STUDENT(1,'徐越',1,'2012-10-01 10:20:01'); 在第二個存儲過程中 ①利用SET聲明了參數,調用了第一個存儲過程 ②在第一個存儲過程中的NAME參數是輸出參數,所以@CLASSNAME這個參數在調用完第一個過程後就被附值 ③最終利用CONCAT拼接SQL語句並傳入參數執行SQL語句 CALL P_INSERT_STUDENT(1,'徐越',1,'2012-10-01 10:20:01');調用存儲過程