儲存過程p_get_class_name是根據輸入的班級號判斷班級名稱
儲存過程p_insert_student是接收輸入的學生資訊,最終將資訊插入學生表。
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');呼叫儲存過程
MySQL帶引數的儲存過程小例子
儲存過程p get class name是根據輸入的班級號判斷班級名稱 儲存過程p insert student是接收輸入的學生資訊,最終將資訊插入學生表。sql view plain copy print?drop procedure if exists p get class name crea...
MySQL帶引數的儲存過程小例子
儲存過程p get class name是根據輸入的班級號判斷班級名稱 儲存過程p insert student是接收輸入的學生資訊,最終將資訊插入學生表。sql view plain copy print drop procedure if exists p get class name crea...
MySQL帶引數的儲存過程小例子
儲存過程p get class name是根據輸入的班級號判斷班級名稱 儲存過程p insert student是接收輸入的學生資訊,最終將資訊插入學生表。sql view plain copy print?drop procedure if exists p get class name crea...