您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2026/1/23 15:32:58发布29次查看
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
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product