您好,欢迎访问一九零五行业门户网

MySQL带参数的存储过程小例子_MySQL

bitscn.com
mysql带参数的存储过程小例子
存储过程p_get_class_name是根据输入的班级号判断班级名称
存储过程p_insert_student是接收输入的学生信息,最终将信息插入学生表。
[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');调用存储过程
bitscn.com
其它类似信息

推荐信息